0

We've a lots of controllers which return JSON result to the View. The view needs to be dynamic based on user's contract type and user's home location.

I'm thinking of passing contract type and other properties using MVC 3.0 ViewBag object. View then access this object and toggle the UI.

Could some one help me if this is the best approach? The draw back of this approach is that every controller needs to pass viewbag to the UI or is it good to store the required object in session and access it from View.

Thanks.

gideon
  • 19,329
  • 11
  • 72
  • 113
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

1 Answers1

3

ViewBagand ViewData are more of a worst approach. The ViewModel should contain everything the View needs.

If you want to know more about the why and how, have a look at this excellent explanation by Darin Dimitrov: ViewBag, ViewData and TempData

Update: In the comments you write that you can't use a ViewModel because you're retuning a JSON result. Passing JSON can be done with a ViewModel as well:

public JsonResult SomeAction()
{
    SomeViewModel model = ...;
    return Json(model, JsonRequestBehavior.AllowGet);
}
Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108