加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – MVC3 IEnumerable模型找不到正确的编辑器模板

发布时间:2020-12-16 03:27:16 所属栏目:asp.Net 来源:网络整理
导读:我有以下型号: public class Filter{ public string Field { get; set; } public string Operator { get; set; } public string Value { get; set; }} 以下控制器: public class FilterController{ public ActionResult Index() { IListFilter model = new
我有以下型号:

public class Filter
{
    public string Field { get; set; }

    public string Operator { get; set; }

    public string Value { get; set; }
}

以下控制器:

public class FilterController
{
    public ActionResult Index()
    {
        IList<Filter> model = new List<Filter>() {
            new Filter(),new Filter()
        };

        return View(model);
    }
}

以下观点:

@model IEnumerable<Filter>
@Html.EditorForModel()

这应该查找我的EditorTemplate Filter.cshtml,并为列表中的每个元素呈现模板,对吧?

使用Glimpse,我注意到MVC正在寻找IEnumerable`1.cshtml而不是Filter.cshtml

我使用时会发生同样的事情

@Html.EditorFor(model => model)

当我这样做:

@Html.EditorFor(model => model,"Filter")

我得到一个错误,说Filter.cshtml期望一个Filter类型的模型,但它收到了IEnumerable类型的模型< Filter>

我这样做了吗?我是否需要做任何其他事情才能使用正确的编辑器模板正确渲染模型列表?

解决方法

我在过去使用EditorTemplates肯定遇到过问题,但我认为这主要是用户错误.

一种可能的解决方法是将集合封装在单个视图模型类中,并将其传递给视图

public class MySweetFilterViewModel
{
    public IEnumerable<Filter> Filters { get; set; }
}

然后,您可以使用单个视图来分离集合

@model Project.Models.MySweetFilterViewModel
@Html.EditorFor(x => x.Filters)

只需确保您的控制器封装

public ActionResult Index()
{
    //...
    return View(new MySweetFilterViewModel() { Filters = model });
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读