代码之家  ›  专栏  ›  技术社区  ›  neopickaze

缺少值属性.net core mvc HiddenFor helper

  •  0
  • neopickaze  · 技术社区  · 7 年前

    如果我手动创建一个隐藏的属性并使用idfornamefor和手动设置值,它将按预期工作。

        // This works and has a value attribute with a value
        <input type="hidden" id="@Html.IdFor(x => x.Fields[componentIndex].Value)" name="@Html.NameFor(x => x.Fields[componentIndex].Value)" value="@Model.Fields[componentIndex].Value"/>
    
        // This has a value attribute but it's empty
        @Html.HiddenFor(x => x.Fields[componentIndex].Value)
    

    请注意,在这两种情况下,生成的名称和id是相同的。

    如果我用一个HiddenFor并提供一个 new { @Value=... } 它也没有设置值。

    设置值的HiddenFor(使用相同的代码但使用不同的组件索引)之间的唯一区别是它们是在post上提供的,而这个有问题的是在控制器中设置的。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Dmitry Pavlov    7 年前

    ASP.NET Core 你可以利用 tag helper asp-for

    <input asp-for="ModelPropertyName" type="hidden" />
    

    POST 名称应遵循集合绑定命名格式,如 Fields[i].Value i 是集合索引。

    Model Binding in ASP.NET Core

    推荐文章