当前位置:  开发笔记 > 后端 > 正文

所有PartialViews的自定义ActionResult等效模型

如何解决《所有PartialViews的自定义ActionResult等效模型》经验,为你挑选了1个好方法。

我创建了以下自定义ActionResult,返回多个部分视图.

public class MultiplePartialViewsResult : ActionResult
{
    private const string Separator = "-";
    private PartialViewResult[] _partialViews;

    public MultiplePartialViewsResult(params PartialViewResult[] partialViews)
    {
        _partialViews = partialViews;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        foreach (var partialView in _partialViews)
        {
            partialView.ExecuteResult(context);
            context.HttpContext.Response.Output.Write(Separator);
        }
    }
}

然后我用它如下:

return new MultiplePartialViewsResult(
             PartialView("~/Views/RowSumView.cshtml", new List() { 1.0 }),
             PartialView("~/Views/ColumnSumView.cshtml", new List() { 2.0 }));

但是,如果我在MultiplePartialViewsResult构造函数中放置断点,我会看到两种情况下的模型等于2.0.它使用数组中指定的最后一个模型覆盖所有模型.

尝试在方法中创建几个PartialViews并将其分配给变量.然后你会注意到他们都拥有相同的模型.



1> Mikhail..:

您可以使用新的ViewData/ViewData.Model实例以下列方式初始化MultiplePartialViewsResult对象(对我来说很好):

return new MultiplePartialViewsResult(
    //PartialView("~/Views/RowSumView.cshtml", new List() { 1.0 }),
    //PartialView("~/Views/ColumnSumView.cshtml", new List() { 2.0 })
    new PartialViewResult() { ViewName = "~/Views/RowSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List() { 1.0 } } },
    new PartialViewResult() { ViewName = "~/Views/ColumnSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List() { 2.0 } } }
);

自定义操作结果:

public class MultiplePartialViewsResult : ActionResult {
    private const string Separator = "-";
    private PartialViewResult[] _partialViews;

    public MultiplePartialViewsResult(params PartialViewResult[] partialViews) {
        _partialViews = partialViews;
    }

    public override void ExecuteResult(ControllerContext context) {
        foreach(var partialView in _partialViews) {
            partialView.ExecuteResult(context);
            context.HttpContext.Response.Output.Write(Separator);
        }
    }
}

控制器:

public ActionResult Index() {
    return View();
}
public ActionResult FakeAction() {
    return new MultiplePartialViewsResult(
        //PartialView("~/Views/RowSumView.cshtml", new List() { 1.0 }),
        //PartialView("~/Views/ColumnSumView.cshtml", new List() { 2.0 })
        new PartialViewResult() { ViewName = "~/Views/RowSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List() { 1.0 } } },
        new PartialViewResult() { ViewName = "~/Views/ColumnSumView.cshtml", ViewData = new ViewDataDictionary() { Model = new List() { 2.0 } } }
    );
}

浏览次数:

指数:

@Html.Action("FakeAction")

ColumnSumView.cshtml/RowSumView.cshtml:

@model List
    @foreach(double item in Model) {
  • @item
  • }


发生这种情况的原因是控制器的[`PartialView`](https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Controller.cs)帮助程序最大限度地用于创建每个部分结果.该帮助器将在ViewData.Model中设置模型,其中ViewData是[控制器]中的属性(https://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/ControllerBase.cs).调用两次`PartialResult`将覆盖ViewData.Model
推荐阅读
mylvfamily
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有