tags:

views:

105

answers:

2

In ASP.NET MVC, how does the controller pass information between the model and the view?

Let's say I have a list of baseball players in my database. After I query those players using LINQ in the controller, how do I then pass this information on to view (my list of baseball players and their stats)?

And after I pass them onto the view, how do I use the inline code in the views html to loop through and display it?

+5  A: 

The controller has a ViewData field that you can use. It's a dictionary and you can use like that:

ViewData["players"] = yourList;

After that you can access the same ViewData on the View:

<? foreach(var player in ViewData["players"] as List) {} ?>

You can also make a strong typed view, changing the view base class from ViewPage to ViewPage < T >, where T is your user-defined class. At the controller you return the class on the return method View() and access it on the view using the ViewModel property.

Augusto Radtke
+1  A: 

Check out the ASP.NET MVC Tutorials at http://www.asp.net/learn/mvc/, in particular the first 3 tutorials on Models, Controllers and Views, and how they interact.

JMs