将对象模型返回到由不同控制器处理的视图
如何将对象模型传递给视图,即母版页上的部分视图?
问候
how can I pass an object model to a view, that is partial view on a master page?
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能会考虑创建另一个对象来更接近地表示您尝试渲染的视图。
假设我有一个
MyDomain.Order
对象,因此我创建了一个类似于ViewPage
的视图页面。现在,假设我有一个由登录用户驱动的菜单,例如。将菜单作为 MyDomain.Order 的属性是没有意义的。我将创建另一个对象,专门用于视图,将其称为OrderPageModel
之类的名称,并将MyDomain.Order
和List
作为属性这个新对象,我的视图被设置为ViewPage
。另一件需要考虑的事情可能是类似
Html.RenderAction()
的事情。同样的情况,我有一个视图,正如您在问题中提到的,它有一个母版页,就像在我的示例中一样,可以说它托管您的网站通用的菜单。您可以创建一个部分视图 (UserMenu.ascx
) 和一个带有计算项目的操作 (UserMenu
) 的控制器 (SiteController.cs
)对于菜单。在母版页中,您可以调用<% Html.RenderAction("UserMenu","SiteController") %>
。如果第一个示例是为特定视图制作的,我会使用第一个示例:只需将其作为模型的一部分即可。如果第二个例子对网站来说更通用,比如菜单,我会使用第二个例子。
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 likeViewPage<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 ofMyDomain.Order
. I would create another object, specifically for the view, call it something likeOrderPageModel
and haveMyDomain.Order
andList<MenuItem>
as properties of this new object, my view being set up asViewPage<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:
这里最好的选择是 RenderAction 而不是 RenderPartial。您的子控制器可以轻松地确定用户是否已登录并呈现正确的部分,而不是让您的母版页担心这些细节。
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.