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

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

问题描述

遵循 NerdDinners 示例,我对创建强类型母版页很感兴趣.为了实现这一点,我使用了一个基本控制器来检索母版页的数据.所有其他控制器都继承了这个类.同样,我有用于母版页和任何其他视图的 ViewModels.视图 ViewModel 类继承自母版页的 ViewModel.

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.

子控制器应该如何确保母版页的数据传递到视图而不设置其与母版页本身相关的 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 文件中确定的,因此是我正在填充的 Buttons 类.

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.

MasterPage ViewModel 代码片段

using System.Collections.Generic;

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

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

视图继承了System.Web.Mvc.ViewMasterPage.

推荐答案

您可以创建一个动作执行后过滤器,它会查找该类型的模型并相应地设置属性,也许是通过调用基本控制器函数.然后您将过滤器放在基类上,所有操作都会自动看到它.

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.

action filter 属性获取控制器的ViewModel,并传递给控制器​​的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:

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

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

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