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?
The controller has a ViewData field that you can use. It’s a dictionary and you can use like that:
After that you can access the same ViewData on the View:
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.