Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why everybody hates ViewData?

I wonder, why everybody hates ViewData so much?
I find it quite useful and convenient. I tell you why: typically every controller action has it's own ViewModel, so it's used only once and I find it very tedious to modify ViewData class every time I need to add extra portion of data to view (adding extra field to class usually leads to modifying its constructor). Instead I can write in controller

ViewData["label"] = someValue;
// in mvc 3 even better:
ViewData.Label = someValue

and in view

<%= ViewData["label"] %>
<%-- mvc 3: --%>
<%= ViewData.Label %>

or for complex types:

<% ComplexType t = (ComplexType)ViewData["label"]; %> // and use all benefits of strong typing 
<%= t.SomeProperty %>

Writing a controller action I do not have to switch to another class, when I need to add some data to view. And a big plus for me: no flooding your project with senseless classes and switching between them and others.
I agree that usage of "magic strings" could lead errors which are not caught by compiler, but these errors localized in very small part of code and can be discovered very quickly. Besides, how do you think guys working with dynamic languages (rails, django) lives without strong typing at all?)

What's your opinion about using ViewData?

like image 215
kilonet Avatar asked Oct 21 '10 13:10

kilonet


People also ask

What is the purpose of ViewData?

ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.

What kind of object is ViewData?

ViewBag, ViewData, and TempData all are Dictionary objects in ASP.NET MVC and these are used for data passing in various situations. The following are the situations where we can use TempData and other objects. Pass the data from Controller to View. Pass the data from an action to another action in Controller.

What is ViewData in razor?

ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel. {


2 Answers

I think this goes beyond just the magic string argument. I would argue that ViewModels are a good thing and not senseless classes because they help keep the Views cleaner and easier to read than accessing ViewData all over the Views.

When you get to five, ten, twenty pieces of data that need to be displayed in a view are you really going to pass all that data over as ViewData? It will make your view harder to follow and that data won't have any meaning. Making a ViewModel and strongly typing the view to that ViewModel will not only make the code easier to read, but you don't have to go casting ViewData objects all over your code.

I do think ViewData is good for certain cases, but when you are dealing with a lot of data it can easily be abused in my opinion.

like image 126
amurra Avatar answered Sep 28 '22 19:09

amurra


Weeellll.....

Why don't you write classes this way?

public dictionary<string, object> myCollectionOfClasses;
myCollectionOfClasses.Add("MyClass", new MyClass);

public class MyClass
{
    public string DoSomething
    {
        return "SomeString";
    }
}

You would have to call them this way:

string myString =  myCollectionOfClasses["MyClass"].DoSomething();

Now which is sillier?

See also
How we do MVC – View models

like image 30
Robert Harvey Avatar answered Sep 28 '22 19:09

Robert Harvey