【问题标题】:Editor method of HtmlHelper class doesn't work with collectionsHtmlHelper 类的编辑器方法不适用于集合
【发布时间】:2019-10-30 11:20:53
【问题描述】:

在我看来,我有一个具有 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-core data-binding


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答。澄清一下,您是在告诉我 Editor 方法不支持集合吗?如果是这样,我将放弃尝试使其工作并找到解决方法。
    • 根据查看html.editor及相关问题的文档,html.editor似乎不支持收藏。您可以尝试改用TextBox。查看我的回复更新。
    猜你喜欢
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    相关资源
    最近更新 更多