New in MVC | Data in the form from different sources
OK, So I watched a few MVC vids and read a few bits. I'm new to the whole MVC pattern and have happily ended up in the world of web forms so far!
As with many demos this all seems great and I'm sure I will have a lot of things that I don't understand as I progress, but in the first case ...
I see that you can have a strongly typed view that receives data from a controller. What happens if I want data in the view from different types of objects? Let's say I want to show a grid of cars and a grid of people that are not related anyway.
thanks Steve
source to share
Set up your strongly typed ViewData class with two properties like
public class MyViewData
{
public IEnumerable<Car> Cars { get; set; }
public IEnumerable<People> People { get; set; }
}
and then fill them in the controller, sorry for the duplicate. In good MVC spirit, try to use interfaces where possible to make your code more general
source to share
Instead of artificially grouping the models together, you could then separate (logically and physically) and then pull the different parts together in the view.
Check out this post for a great explanation [link text] [1].
[1]: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ partial requests
source to share
In this situation, your best bet would be to create a class in the Models folder to store both of these types.
Example:
public class CarsPeopleModel
{
public List<Car> Cars { get; set; }
public List<Person> People { get; set; }
}
Then your view will look like this:
public partial class Index : ViewPage<MvcApplication1.Models.CarsPeopleModel>
{
}
source to share