MVC multiple data-models passed to a view [duplicate]
Possible Duplicate:
ASP.NET MVC - View with multiple models
Using ASP.NET MVC 3.0, how can I pass multiple data models from my controller to my view? I'm hoping for an elegant solution...thanks!
BTW - I can provide code samples but I think this is generic enough not to warrant it
There isn't a truly elegant way to do it, unfortunately. My preferred method is to encapsulate the two objects and send that to the view.
public class City{
public Mall TheMall;
public School TheSchool;
}
Then your view will be strongly typed as City, and you will use Model.TheMall.Property
and Model.TheSchool.Property
to access what you need.
Another method is to use the ViewData bag. What you can do is populate it like such:
ViewData["City"] = "Toronto"; //random string
ViewData["Mall"] = new TheMall(...);
return View(MyViewName);
And then in your view, you will have access these using the key you assigned in the controller ("City", "Mall", in this case).
Hope that helps!
精彩评论