如何在ASP.NET MVC中使用基本控制器来创建一个强类型的母版页 [英] How to create a strongly typed master page using a base controller in ASP.NET MVC

查看:923
本文介绍了如何在ASP.NET MVC中使用基本控制器来创建一个强类型的母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继<一个href=\"http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx\">NerdDinners例如,我感兴趣的是建立一个强类型的母版页。为了实现这一点,我用一个基本控制器以检索数据的母版页。所有其他控制器继承这个类。同样,我有的ViewModels 母版页和任何其他意见。视图视图模型类继承母版页的视图模型

Following the NerdDinners example, I am interested in creating a strongly typed Master Page. In order to achieve this, I use a base controller which retrieves the data for the master page. All other controllers inherit this class. Similarly, I have ViewModels for the master page and any other views. The view ViewModel classes inherit from the master page's ViewModel.

孩子控制器应如何确保该母版页的数据传递给视图而不设置的属性,其视图模型,涉及到母版页本身呢?

How should a child controller ensure that the master page's data is passed to the View without setting the properties of its ViewModel that pertain to the master page itself?

我的母版页会显示一个数字按钮,这是在一个XML文件中确定的,因此按钮,我填充类。

My the master page will display a number of buttons, which are determined in an XML file, hence the Buttons class that I am populating.

母版视图模型code段

using System.Collections.Generic;

namespace Site1.Models
{
    public class MasterViewModel
    {
        public List<Button> Buttons{set; get;}
    }
}

查看视图模型

View ViewModel

namespace Site1.Models
{
    public class View1ViewModel : MasterViewModel
    {
        public SomeDataClass SomeData { get; set; }
    }
}

基础控制器

using System.Collections.Generic;
using System.Web.Mvc;
using Site1.Models;

namespace Site1.Controllers
{
    public abstract class BaseController : Controller
    {
        protected MasterViewModel model = new MasterViewModel();

        public BaseController()
        {
            model.Buttons = new List<Button>();
            //populate the button classes (doesn't matter how)
            PopulateButtons(model.Buttons);
        }
    }
}

视图的控制器:

using System.Web.Mvc;

namespace Site1.Controllers
{
    public class View1Controller : BaseController
    {
        public ActionResult Index()
        {
            Models.View1ViewModel viewModel = new Models.View1ViewModel();
            SomeDataClass viewData = new SomeDataClass()
            //populate data class (doesn't matter how)
            PopulateDataClass(viewData);
            viewModel.SomeData = viewData;
            //I WANT TO ELIMINATE THE FOLLOWING LINE!
            viewModel.Buttons = model.Buttons;
            return View("Index", viewModel);
        }
    }
}

母版页继承 System.Web.Mvc.ViewMasterPage&LT; Site1.Models.MasterViewModel方式&gt;

视图继承 System.Web.Mvc.ViewMasterPage&LT; Site1.Models.View1ViewModel方式&gt;

推荐答案

您可以创建一个行动后执行的过滤器,查找该类型的模型,并设置相应的属性,也许是通过调用基地控制器功能。然后,您可以把基类中的过滤器,所有的行动将自动看到它。

You could create an after action executed filter which looks for a model of that type and sets the properties accordingly, perhaps by calling a base controller function. You would then put the filter on the base class, and all actions would see it automatically.

行动过滤属性得到控制器的视图模型,并将其传递给控制器​​的则setModel 功能:

The action filter attribute gets the controller's ViewModel, and passes it to the controller's SetModel function:

using System.Web.Mvc;
using Site1.Controllers;

namespace Site1.Models
{
    public class MasterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);

            MasterViewModel viewModel = (MasterViewModel)((ViewResultBase)filterContext.Result).ViewData.Model;

            BaseController controller = (BaseController)filterContext.Controller;
            controller.SetModel(viewModel);
        }
    }
}

此功能被添加到 BaseController

This function is added to the BaseController:

public void SetModel(MasterViewModel childViewModel)
{
    childViewModel.Buttons = model.Buttons;
}

这篇关于如何在ASP.NET MVC中使用基本控制器来创建一个强类型的母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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