ASP.NET MVC – Passing Data Around

I’ve made no secrets of the fact that I am n00btaculous with regard to ASP.NET.  The first week or so with the MVC was pretty painful for the simple fact that the available information online is somewhat dated.  That sounds weird when talking about something that hasn’t even shipped yet, but here’s what I mean.  If you do a search for “ASP.NET MVC”, you will find that some of the links head off to Scott Guthrie’s blog.  This is a good thing, since he’s the big cheese here at Microsoft responsible for a great many things, including MVC.  Here’s the problem: that link is from a blog post in late 2007.  As a n00b, I’m wasn’t sure whether or not anything has changed, especially since we are now at a release candidate for ASP.NET MVC, rendering the code samples useless in my mind.

With that in mind, I have been scrabbling around looking for information.  To quote Jaime Escalante, I was “like a blind man, in a dark room, looking for a black cat that’s not even there.”

It turns out that the ASP.NET site completely redid the samples that they had for MVC, which is pretty awesome when you think about it.  Head’s up, it’s easy to get lost in the site and not find the “new hotness.”  You want this link to their MVC content, and not this link off their “Learn” tab.

Now, to the task at hand.  With the Controllers and Views, you can separate your data from the presentation.  Awesome.  How do I get the data from Controller to View?  When I first started learning, it wasn’t clear that I could pass more than one piece of data, because most of the samples I saw had something along the lines of:

return View(data);

where “data” could be any single object.  As with any function, you wouldn’t expect to be able to return multiple objects.  So, n00b that I am, I was trying to create all kinds of complicated structures to pass different data back.  For example, I did something like:

List<Dictionary<string, int>> myData;

and proceeded to dump data into this complicated structure.  Don’t do this.  I then tried to create custom classes for returning different sets data as one repository.  Please don’t do that.  There is a much, much simpler solution, which I missed in the tutorial sets, because it’s not clearly called out as to what they are doing.  You can pass whatever data you want back to your view, and label it.  I took advantage of this as follows:

ViewData["myDict1"] = myDict1;
ViewData["myDict2"] = myDict2;
return View();

Once you do that, your View can access the data as follows:

<% foreach (KeyValuePair<string,int>kvp in (Dictionary<string,int>)ViewData["MyDict1"]) %>
<% { %>
<%= String.Format("Key: {0}, Value: {1}",kvp.Key, kvp.Value) %>
<% } %>

You can replace the “myDict1” with “myDict2” if you want to access that data set.  The data structures can be whatever you want – I just used two dictionaries for illustrative purposes.

  • Pingback: ASP.NET MVC Archived Buzz, Page 1()

  • Evan

    I prefer strongly-typed views and passing a single view-model object that contains my business entities and other stuff I want to pass. The ViewData approach is error prone and could break during refactoring plus you get no intellisense.