Viewbag Life Cycle in C# MVC

Asked

Viewed 92 times

0

I know the life cycle of ViewBag of C# MVC, is by request.

Doubt

If I set one ViewBag with the same name in several controllers, there is the possibility of the set information being read / misinterpreted in View?

Example

  • Request X access to ControllerA ago ViewBag.Mensagem = "SOP"
  • Y request access to ControllerA ago ViewBag.Mensagem = "SO"
  • Request W access to ControllerB ago ViewBag.Mensagem = "SOPortuguês"

However, the Y request is rendered first, so it occurs when using the ViewBag.Mensagem in View, turn up SOPortuguês instead of SO?

1 answer

0

  1. One ViewBag transfers data from controller to view, ideally temporary data that is not included in a model.
  2. The life of a ViewBag lasts only during the current http request. The values of a ViewBag will be null if redirection occurs.

That is, the value of ViewBag is unique within a Request, so for each call you will have a different value in your case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.