Parameter passing with Asp.Net MVC

12

In asp.Net MVC .

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

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

Is there a performance difference between them?

    
asked by anonymous 29.08.2016 / 14:36

1 answer

17

ViewData

This is a temporary dictionary of values used to pass auxiliary information between Controller and View . It exists only during request processing.

This is the most rudimentary way to pass data between Controller and View . Your variables require type conversion to be used. It exists since the first version of ASP.NET MVC.

ViewBag

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

In .NET, dynamic is always slower, but the difference in processing between ViewData and ViewBag is barely noticeable (unless, of course, you pass a gigantic object by ViewBag , which, besides being a bad practice, is rare to happen).

This is the form that applies to the MVC5 for passing accessory values between Controller and View .

View Tipada

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

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

The View Model should contain all the essential information to compose the presentation. In the case of Views that modify data, all data that must be modified through View must be within Model / em>.

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

About Recommended Uses

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

  • Populate DropDowns options
  • Fill in some screen information that is not part of the essential View data;
  • Pass some message, such as a notification or a successful message type.

On performance, strong typing always prevails over dynamic typing. As a good practice, it's always worth converting everything for strong typing if possible.

    
29.08.2016 / 15:16