首页 > 解决方案 > HtmlHelper 类的编辑器方法不适用于集合

问题描述

在我看来,我有一个具有 List 类型的属性 Details 的模型。该系列有 3 个元素。现在我需要在视图中编辑这个列表。如果我使用 Html.EditorFor 方法传递表达式一切正常,但如果我使用 Html.Editor 方法,绑定失败。“失败”是指 MVC 对所有将 null 作为模型传递的字段(即使它们是数字)使用字符串编辑器。

    // this works correctly
    @for (var i = 0; i < Model.Details.Count; i++)
    {
      <li>
        @Html.EditorFor(m => m.Details[i].Name)
        @Html.EditorFor(m => m.Details[i].Age)
      </li>
    }


    // this doesn't work
    @for (var i = 0; i < Model.Details.Count; i++)
    {
      <li>
        @Html.Editor("Details[" + i +"].Name")
        @Html.Editor("Details[" + i +"].Age")
      </li>
    }

我正在使用 ASP.NET Core 3.0,并且没有针对以前的版本测试此代码。由于多种原因,我无法使用 EditorFor 方法,所以我遇到了这个问题。

有任何想法吗?

标签: asp.net-coredata-binding

解决方案


Editor() HTML Helper 方法用于简单类型视图,EditorFor() HTML Helper 方法用于强类型视图,根据模型对象属性的数据类型生成 HTML 元素。

的定义Html.Editor

    // Summary:
    //     Returns HTML markup for the expression, using an editor template. The template
    //     is found using the expression's Microsoft.AspNetCore.Mvc.ModelBinding.ModelMetadata.
    //
    // Parameters:
    //   htmlHelper:
    //     The Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper instance this method extends.
    //
    //   expression:
    //     Expression name, relative to the current model. May identify a single property
    //     or an System.Object that contains the properties to edit.
    //
    // Returns:
    //     A new Microsoft.AspNetCore.Html.IHtmlContent containing the <input> element(s).
    //
    // Remarks:
    //     For example the default System.Object editor template includes <label> and <input>
    //     elements for each property in the expression's value.
    //     Example expressions include string.Empty which identifies the current model and
    //     "prop" which identifies the current model's "prop" property.
    //     Custom templates are found under a EditorTemplates folder. The folder name is
    //     case-sensitive on case-sensitive file systems.
    public static IHtmlContent Editor(this IHtmlHelper htmlHelper, string expression);

您可以为 Editor Tag Helper 的表达式识别单个属性,如下所示:

@model MVC3_0.Models.Detail
<table>
<tr>
    <td>Id</td>
    <td>@Html.Editor("Id")</td>
</tr>
<tr>
    <td>Name</td>
    <td>@Html.Editor("Name")</td>
</tr>
<tr>
    <td>Age</td>
    <td>@Html.Editor("Age")</td>
</tr>
</table>

public IActionResult Index()
    {
        var model = new Detail { Id = 1, Name = "jack", Age = 12 };
        return View(model);
    }

结果: 在此处输入图像描述

有一种解决方法,您可以使用TextBoxinput代替

@for (var i = 0; i < Model.Details.Count; i++)
{
<li>
    @Html.TextBox("Details[" + i + "].Name", Model.Details[i].Name, new { htmlAttributes = new { @class = "text-field" } })
    @Html.TextBox("Details[" + i + "].Age", Model.Details[i].Age, new { htmlAttributes = new { @class = "text-field" } })

</li>
}

// input tag helper
@for (var i = 0; i < Model.Details.Count; i++)
{
<li>
    <input asp-for="@Model.Details[i].Name" />
    <input asp-for="@Model.Details[i].Age" />
</li>
}

推荐阅读