ASP.NET MVC母版页数据 [英] ASP.NET MVC Master Page Data

查看:156
本文介绍了ASP.NET MVC母版页数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我越使用ASP.NET MVC,我就越喜欢它。然而,在显示在母版页模型数据的情况下,似乎有这样做的几种方法。我不能确定最佳的解决方案。

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.

在asp.net web表单我通常会做到这一点使用的用户控制各做各的绑定来获取所需的数据。

In asp.net web forms I would typically do this using user controls each doing their own databinding to retrieve the required data.

在MVC中的所有数据应该由控制器传递

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();

但是,这样做的每一个动作不是很干那么以下这篇文章我创建了一个基本控制器,增加了所需要的数据,以我的ViewData:

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();
    }
}

然后我创建了一个扩展ViewMasterPage:

I then created an extension for ViewMasterPage:

        public static void RenderCategoryList(this ViewMasterPage pg) {
        pg.Html.RenderPartial("CategoryList", pg.ViewData["Categories"]);
    }

在我的母版:

        <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.

关于车状态,我想我会做类似的事情,但我不确定是否的RenderAction会更合适(的何时使用VS的RenderAction使用的RenderPartial ASP.NET MVC )。
谢谢,

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

推荐答案

这样的作品,虽然它不是我会做有两个原因方式:

That works, although it's not the way I would do it for 2 reasons:


  1. 我不喜欢粘数据转换成ViewState的,因为你基本上是将它转换为对象

  2. 将要求你限制功能,从这个basecontroller继承控制器基控制器(这可能不是一个问题,虽然)

我认为这将是一个完美的使用的RenderAction (该MvcFutures项目的一部分)的。这个助手可以用来渲染另一个控制器上的动作。所以,你可能有一个ListCategories行动ProductController的,你可能只是这样做:

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:

<% Html.RenderAction<ProductController>(x => x.ListCategories()); %>

ListCategories会叫

ListCategories would call

_service.GetCategories();

和可能粘在信息变成自己的模型。那就该模型传递给视图就会将部分页面。

and might stick the info into its own Model. Then it would pass that model to the View would would be a Partial Page.

这篇关于ASP.NET MVC母版页数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆