HtmlHelper类的编辑器方法不适用于集合

时间:2019-10-29 08:42:29

标签: asp.net-core data-binding

在我看来,我有一个模型,其属性为List类型。该集合包含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方法,因此我陷入了这个问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  

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);
    }

结果: enter image description here

有一种解决方法,您可以改用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>
}