Returning object model to a view, that is handled by different controller
regards
You might consider creating an another object that more closely represents the view you are trying to render.
Let's say i have an MyDomain.Order
object, so I make a view page that looks something like ViewPage<MyDomain.Order>
. Now, let's say that I have a menu that is driven off of a logged in user, as example. It wouldn't make sense to have menu as a property of MyDomain.Order
. I would create another object, specifically for the view, call it something like OrderPageModel
and have MyDomain.Order
and List<MenuItem>
as properties of this new object, my view being set up as ViewPage<OrderPageModel>
.
The other thing to consider might be something like Html.RenderAction()
. Same scenario, I have a view, and as you mention in your question, it has a master page, and as in my example, lets say it hosts a menu common to your site. You could create a partial view (UserMenu.ascx
) and a controller (SiteController.cs
) with an action (UserMenu
) that calculates the items for the menu. Inside your master page, you can then call <% Html.RenderAction("UserMenu","SiteController") %>
.
I would use the first example if it could be something made for a particular view: just make it a part of the model. I would use the second example if it was something more generic to the site, like a menu.
You could specify the location of the view:
return PartialView("~/Views/SomeOtherController/SomePartial.ascx", someModel);
Best bet here is RenderAction over RenderPartial. Your child controller can easily figure out if the user is logged in and render the right partial rather than making your master page worry about these details.
精彩评论