2

I have a fairly simple question: Is ViewBag an ugly solution? I have the option to add an extra property to my viewmodel to store the simple int in and the option to pass the integer to the view by storing it in ViewBag. ViewBag is obviously much simpler, especially since i don't have to reuse the integer and only need it once in the whole process.

In my opinion though, Viewbag is an extremely ugly solution. Is just seems so clunky and amateuristic.

Is it really? Or is it a really acceptable solution to a problem like this?

Mick
  • 99
  • 10
  • 1
  • 2
    This is probably too opinion based as written. That said, my opinion is, ViewBag should only be used for POC / research / quick jobs and not in a robust production system. – stephen.vakil Sep 28 '16 at 13:36
  • Got me wondering actually. To myself i guess. I just think it's an ugly solution, and i'm wondering if it's acceptable at like a code review or something. – Mick Sep 28 '16 at 13:36
  • 1
    http://stackoverflow.com/questions/11262034/mvc-viewbag-best-practice – Jonesopolis Sep 28 '16 at 13:37
  • 2
    `ViewBag` is a dynamically typed object. It's exactly the kind of thing that is easy to start using but can eventually bite you later on. You can do whatever you want at all times, but if it seems ugly to you, don't use it. – Theodoros Chatzigiannakis Sep 28 '16 at 13:37
  • The `ViewBag` confused me more than anything else in MVC due to the habit of transporting/translating strongly typed classes to and from the layers. I think the `ViewBag` was intended to transport ad-hoc whatever you want to display to a view. I may be wrong, however, I have never used the `ViewBag`. – Ross Bush Sep 28 '16 at 13:38
  • ViewBag I tend to use more for temporary stores between the controller and the View, for example user specific objects you don't necessarily want to tie to a model. For more complex objects I say stick to having a concrete model class. Much easier to support rather than looking around for magic strings. – nik0lai Sep 28 '16 at 13:39
  • 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. ViewModel is better option to pass data which has properties specific to the view. – Sunil Kumar Sep 28 '16 at 13:41
  • I agree with @stephen.vakil -- don't use it in anything you'll need to maintain if you can possibly help it. – 15ee8f99-57ff-4f92-890c-b56153 Sep 28 '16 at 13:42
  • Great comments guys, i love to hear all these different point of views. I'm currently only using it for displaying a simple error message, although my opinion stands. It's ugly code. – Mick Sep 28 '16 at 13:46
  • If it's an error message, you could just use `ModelState.AddModelError()`. – Drew Kennedy Sep 28 '16 at 13:50

0 Answers0