@Html.HiddenFor returning null value

后端 未结 1 348
北荒
北荒 2020-12-12 07:46

I am trying to return the results of a table back to the controller for further manipulation. Once returned to the controller the value shows as null. In the past I have b

相关标签:
1条回答
  • 2020-12-12 08:21

    You cannot use ElementAt() in a HtmlHelper method that generates form controls (look at the name attribute your generating - it does not match your model).

    Either change the model to be IList<T>

    @model List<Project.Models.Item>
    

    and use a for loop

    @for (int i = 0; i < Model.Count; i++)
    {
        ....
        @Html.HiddenFor(m => m.[i].Name)
        ....
    

    or change use a custom EditorTemplate for typeof Item, and in the main view, use @Html.EditorFor(m => m) to generate the correct html for each item in the collection.

    0 讨论(0)
提交回复
热议问题