Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC ViewBag Best Practice [closed]

For the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model?

Question:

  1. Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view model)

  2. Are there situations where a ViewBag is absolutely necessary?

like image 722
Nate Pet Avatar asked Jun 29 '12 12:06

Nate Pet


People also ask

Which is faster ViewData or ViewBag?

ViewBag will be slower than ViewData; but probably not enough to warrant concern.

Is it good to use ViewBag?

Yes, the ViewBag is bad. Strong typing is a best practice for many reasons (which you can research for yourself). I would use the ViewBag very sparingly.

Should I use ViewBag or ViewData?

ViewData and ViewBag are used for the same purpose -- to transfer data from controller to view. ViewData is nothing but a dictionary of objects and it is accessible by string as key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData.

Is performance of ViewBag slower than ViewData in MVC?

In general, ViewBag is a way to pass data from the controller to the view. It is a type object and is a dynamic property under the controller base class. Compared to ViewData, it works similarly but is known to be a bit slower and was introduced in ASP.NET MVC 3.0 (ViewData was introduced in MVC 1.0).


2 Answers

ViewBag is a dynamic dictionary. So when using ViewBag to transfer data between action methods and views, your compiler won't be able to catch if you make a typo in your code when trying to access the ViewBag item in your view. Your view will crash at run time :(

Generally it is a good idea to use a view model to transfer data between your action methods and views. view model is a simple POCO class which has properties specific to the view. So if you want to pass some additional data to view, Add a new property to your view model and use that.Strongly typed Views make the code cleaner and more easy to maintain. With this approach, you don't need to do explicit casting of your viewbag dictionary item to some types back and forth which you have to do with view bag.

public class ProductsForCategoryVm {   public string CategoryName { set;get; }   public List<ProductVm> Products { set;get;}     } public class ProductVm {   public int Id {set;get;}    public string Name { set;get;} } 

And in your action method, create an object of this view model, load the properties and send that to the view.

public ActionResult Category(int id) {   var vm= new ProductsForCategoryVm();   vm.CategoryName = "Books";   vm.Products= new List<ProductVm> {      new ProductVm { Id=1, Name="The Pragmatic Programmer" },      new ProductVm { Id=2, Name="Clean Code" }   }   return View(vm); } 

And your view, which is strongly typed to the view model,

@model ProductsForCategoryVm <h2>@Model.CategoryName</h2> @foreach(var item in Model.Products) {     <p>@item.Name</p> } 

Dropdown data ?

A lot of tutorials/books has code samples which uses ViewBag for dropdown data. I personally still feel that ViewBag's should not be used for this. It should be a property of type List<SelectListItem> in your view model to pass the dropdown data. Here is a post with example code on how to do that.

Are there situations where a ViewBag is absolutely necessary?

There are some valid use cases where you can(not necessary) use ViewBag to send data. For example, you want to display something on your Layout page, you can use ViewBag for that. Another example is ViewBag.Title (for the page title) present in the default MVC template.

public ActionResult Create() {    ViewBag.AnnouncementForEditors="Be careful";    return View(); } 

And in the layout, you can read the ViewBag.AnnouncementForEditors

<body> <h1>@ViewBag.AnnouncementForEditors</h1> <div class="container body-content">     @RenderBody() </div> </body> 
like image 67
Shyju Avatar answered Sep 17 '22 05:09

Shyju


1) Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view model)

You should use viewmodels instead of passing data via ViewBag as much as possible.

2) Are there situations where a ViewBag is absolutely necessary?

There is no situation where a ViewBag is absolutely necessary. However, there are some data I personally prefer using ViewBag instead of View Model. For example, when I need to populate a dropdown box for predefined values (i.e Cities), I use ViewBag for carrying SelectListItem array to view. I prefer not to pollute my ViewModels with this data.

like image 35
SadullahCeran Avatar answered Sep 21 '22 05:09

SadullahCeran