ASP.NET MVC 母版页数据
我使用 ASP.NET MVC 越多,我就越喜欢它。然而,在母版页上显示模型数据的情况下,似乎有多种方法可以实现。我不确定最佳解决方案。
我的示例是一个商业网站,我想在每个页面上输出产品类别列表,并显示访问者购物车的状态。
在 ASP.NET Web 表单中,我通常会使用每个用户控件执行自己的数据绑定来检索所需的数据。
在 MVC 中,所有数据都应该由控制器传递。
因此,关于类别,最简单的解决方案似乎是在控制器操作中的“查看数据”中传递此信息:
ViewData["Categories"] = _service.GetCategories();
但是,为每个操作执行此操作并不是很干燥,因此遵循 这篇文章 我创建了一个基本控制器,将所需的数据添加到我的 ViewData 中:
public class AppController : Controller
{
IAppService _service;
public AppController() { }
public AppController(IAppService appService)
{
_service = appService;
SetSiteData();
}
private void SetSiteData()
{
ViewData["Categories"] = _service.GetCategories();
}
}
我为 ViewMasterPage 创建了一个扩展:
public static void RenderCategoryList(this ViewMasterPage pg) {
pg.Html.RenderPartial("CategoryList", pg.ViewData["Categories"]);
}
然后 我的 MasterPage:
<div>
<%this.RenderCategoryList(); %>
</div>
这似乎是一个相当干净的方法。然而,这是最好的方法吗,因为我也看到了为母版页创建 ViewModel 的建议。我可以看到,随着 ViewModel 数据的增长,这可能是一个更好的解决方案。
关于购物车状态,我想我会做类似的事情,但不确定 RenderAction 是否更合适(何时在 ASP.NET MVC 中使用 RenderAction 与 RenderPartial)。 谢谢, 本
The more I use ASP.NET MVC, the more I love it. However, in the case of showing model data on master pages there seems several ways of doing it. I am unsure as to the best solution.
My example would be a commerce site where I want to output a list of product categories on every page and also show the status of the visitors cart.
In asp.net web forms I would typically do this using user controls each doing their own databinding to retrieve the required data.
In MVC all data should be passed by the controller.
So regarding the categories the simplest solution would seem to be to pass this in View data in the controller action:
ViewData["Categories"] = _service.GetCategories();
However, doing this for every action isn't very DRY so following this article I created a base controller that adds the required data to my ViewData:
public class AppController : Controller
{
IAppService _service;
public AppController() { }
public AppController(IAppService appService)
{
_service = appService;
SetSiteData();
}
private void SetSiteData()
{
ViewData["Categories"] = _service.GetCategories();
}
}
I then created an extension for ViewMasterPage:
public static void RenderCategoryList(this ViewMasterPage pg) {
pg.Html.RenderPartial("CategoryList", pg.ViewData["Categories"]);
}
And in my MasterPage:
<div>
<%this.RenderCategoryList(); %>
</div>
This seems quite a clean approach. However, is this the best way as I have also seen suggestions of creating a ViewModel for your MasterPage. I could see that perhaps as your ViewModel data grows, this may be a better solution.
Regarding the cart status, I suppose I would do something similar but am unsure whether RenderAction would be more appropriate (When to use RenderAction vs RenderPartial with ASP.NET MVC).
Thanks,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是可行的,尽管这不是我这样做的方式,原因有两个:
我认为这将是 RenderAction (MvcFutures 项目的一部分)的完美使用。该助手可用于在另一个控制器上呈现操作。因此,您可能有一个具有 ListCategories 操作的 ProductController,您可以执行以下操作:
ListCategories 会调用
并可能将信息粘贴到其自己的模型中。然后它将将该模型传递给视图,这将是一个部分页面。
That works, although it's not the way I would do it for 2 reasons:
I think this would be a perfect use of RenderAction (part of the MvcFutures project). This helper can be used to render an Action on another controller. So you might have a ProductController with a ListCategories action, you could just do something like:
ListCategories would call
and might stick the info into its own Model. Then it would pass that model to the View would would be a Partial Page.
谢谢 - RenderAction 的工作非常完美。
我从此处获得了更多信息。
因此,为了他人的利益,这里是我如何输出购物车状态的示例:
操作:
部分视图(绑定到 Models.Cart)
母版页的帮助方法:
最后是母版页:
谢谢您的建议。
Thank you - RenderAction was perfect the job.
I got more information from here.
So for the benefit of others, here is an example of how I am outputting cart status:
Action:
Partial View (bound to Models.Cart)
Helper method for Master Page:
And finally master page:
Thank you for the advice.