EditorFor IEnumerable <T> with TemplateName

Zab*_*sky 21 c# asp.net-mvc razor asp.net-mvc-3 asp.net-mvc-4

假设我有一个简单的模型来解释目的:

public class Category
{
    ...
    public IEnumerable<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model Category
...
<ul>
    @Html.EditorFor(m => m.Products)
</ul>
Run Code Online (Sandbox Code Playgroud)

EditorTemplate:

@model Product
...
<li>
    @Html.EditorFor(m => m.Name)
</li>
Run Code Online (Sandbox Code Playgroud)

请注意,我不必定义EditorTemplate IEnumerable<Product>,我只能为Product模型创建它,而MVC框架足够聪明,可以使用自己的IEnumerable模板.它遍历我的集合并调用我的EditorTemplate.

输出html将是这样的

...
<li>
    <input id="Products_i_Name" name="Products[i].Name" type="text" value="SomeName">
</li>
Run Code Online (Sandbox Code Playgroud)

毕竟,我可以发布到我的控制器.

但是,当我使用模板名称定义EditorTemplate时,为什么MVC没有做到这一点?

@Html.EditorFor(m => m.Products, "ProductTemplate")
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我必须更改属性的类型,IList<Product>自己遍历集合并调用EditorTemplate

@for (int i = 0; i < Model.Products.Count; i++)
{
    @Html.EditorFor(m => m.Products[i], "ProductTemplate")
}
Run Code Online (Sandbox Code Playgroud)

这似乎是一种肮脏的解决方法给我.这样做是否还有其他更清洁的解决方案?

Dav*_*xas 16

在那里,现在我只欠Darin 9999啤酒.

    public static MvcHtmlString EditorForMany<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, IEnumerable<TValue>>> expression, string templateName = null) where TModel : class
    {
        StringBuilder sb = new StringBuilder();

        // Get the items from ViewData
        var items = expression.Compile()(html.ViewData.Model);
        var fieldName = ExpressionHelper.GetExpressionText(expression);
        var htmlFieldPrefix = html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix;
        var fullHtmlFieldPrefix = String.IsNullOrEmpty(htmlFieldPrefix) ? fieldName : String.Format("{0}.{1}", htmlFieldPrefix, fieldName);
        int index = 0;

        foreach (TValue item in items)
        {
            // Much gratitude to Matt Hidinger for getting the singleItemExpression.
            // Current html.DisplayFor() throws exception if the expression is anything that isn't a "MemberAccessExpression"
            // So we have to trick it and place the item into a dummy wrapper and access the item through a Property
            var dummy = new { Item = item };

            // Get the actual item by accessing the "Item" property from our dummy class
            var memberExpression = Expression.MakeMemberAccess(Expression.Constant(dummy), dummy.GetType().GetProperty("Item"));

            // Create a lambda expression passing the MemberExpression to access the "Item" property and the expression params
            var singleItemExpression = Expression.Lambda<Func<TModel, TValue>>(memberExpression,
                                                                               expression.Parameters);

            // Now when the form collection is submitted, the default model binder will be able to bind it exactly as it was.
            var itemFieldName = String.Format("{0}[{1}]", fullHtmlFieldPrefix, index++);
            string singleItemHtml = html.EditorFor(singleItemExpression, templateName, itemFieldName).ToString();
            sb.AppendFormat(singleItemHtml);
        }

        return new MvcHtmlString(sb.ToString());
    }
Run Code Online (Sandbox Code Playgroud)


Dar*_*rov 14

这样做是否还有其他更清洁的解决方案?

简单的答案是否定的,它很糟糕,我完全赞同你,但这就是框架的设计者决定实现这个功能的方式.

所以我所做的就是坚持惯例.由于我有每个视图和局部视图的特定视图模型,因此使用相应的编辑器模板并不是什么大问题,其命名方式与集合的类型相同.