将对象模型返回到由不同控制器处理的视图

发布于 2024-09-30 22:36:22 字数 47 浏览 3 评论 0原文


如何将对象模型传递给视图,即母版页上的部分视图?

问候

how can I pass an object model to a view, that is partial view on a master page?

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给我一枪 2024-10-07 22:36:22

您可能会考虑创建另一个对象来更接近地表示您尝试渲染的视图。

假设我有一个 MyDomain.Order 对象,因此我创建了一个类似于 ViewPage 的视图页面。现在,假设我有一个由登录用户驱动的菜单,例如。将菜单作为 MyDomain.Order 的属性是没有意义的。我将创建另一个对象,专门用于视图,将其称为 OrderPageModel 之类的名称,并将 MyDomain.OrderList 作为属性这个新对象,我的视图被设置为 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 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.

挽你眉间 2024-10-07 22:36:22

您可以指定视图的位置:

return PartialView("~/Views/SomeOtherController/SomePartial.ascx", someModel);

You could specify the location of the view:

return PartialView("~/Views/SomeOtherController/SomePartial.ascx", someModel);
温柔女人霸气范 2024-10-07 22:36:22

这里最好的选择是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文