开发者

How to return two types of objects in an ASP.net MVC view

I have say class/ojbect one which I am returning as details. But then there is a second class that is related to the first that I want to list (and have an option to look at the details). How do I get return both of those?

Exa开发者_开发知识库mple is like in an item as the main piece but there may be details or other items that are related to the main item. I will select them with a linq query but then how do I return them to the same view?

Do I need to have some sort of partial or other form to display the second object type? I don't see where returning the view I can return both.

My thought for right now is to create a new class that contains both of the object types but I know there has to be a better way.


My thought for right now is to create a new class that contains both of the object types but I know there has to be a better way.

That is the right way. Create a ViewModel class that contains all the data that view needs and pass it to view.


You can either pass it to View through ViewState or ViewBag (depending on which MVC version you are using).


for the first object "main object" return it as the model of this view and for the details put them into ViewState and cast them back within the view

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜