开发者

MVC multiple data-models passed to a view [duplicate]

开发者 https://www.devze.com 2023-04-10 10:27 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_高级运维 Possible Duplicate: ASP.NET MVC - View with multiple models
This question already has answers here: Closed 11 years ago. 开发者_高级运维

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消