Passing parameters with Asp.Net MVC

Asked

Viewed 480 times

12

In asp.Net MVC.

What’s the difference between using ViewBag, ViewData e View Tipada?

And when we should use them, there is some specific situation?

There is a difference in performance between them?

1 answer

17


ViewData

It is a temporary dictionary of values that serves to pass auxiliary information between Controller and View. Exists only during request processing.

It’s the most rudimentary way to pass data between Controller and View. Its variables require conversion of types to be used. It exists since the first version of ASP.NET MVC.

ViewBag

Shape similar to ViewData, but taking advantage of the resource dynamic native introduced in . NET 4 onwards. Simpler to use, does not necessarily require conversion to more complex types.

No . NET, dynamic is always slower, but the difference in processing between ViewData and ViewBag is almost imperceptible (unless, of course, you pass a gigantic object through ViewBag, which, in addition to being a bad practice, is rare to happen).

It is the form that runs until the MVC5 for passage of accessory values between Controller and View.

View Tipada

Here I think the correct expression would be "Model of View", which can actually be any class.

By design, a View It only takes one class. When two or more classes are required to compose the presentation, another class containing all other classes is used, and that class containing the others is passed to the View.

The Model of View shall contain all the information needed to compose the presentation. In the case of Views modifying data, all data to be modified through View need to be inside the Model of View.

Another example is search form screens, which do not exactly alter data, but need to establish a state of research between the application and the View. I explain it here.

On Recommended Uses

ViewData and ViewBag should be used for, among other uses:

  • Fill out options of Dropdowns;
  • Fill in some screen information that is not part of the essential data of View;
  • Pass a message, such as a notification or a success message.

On performance, strong typing always prevails over dynamic typing. As good practice, it is always worth converting everything to strong typing, if possible.

Browser other questions tagged

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