@Html.HiddenFor does not work on Lists in ASP.NET MVC

后端 未结 13 847
灰色年华
灰色年华 2020-11-27 15:13

I\'m using a model that contains a List as a property. I\'m populating this list with items i grab from SQL Server. I want the List to be hidden in the view and passed to th

相关标签:
13条回答
  • 2020-11-27 15:38

    Another option would be:

    <input type="hidden" value=@(string.Join(",", Model.MyList)) />
    
    0 讨论(0)
  • 2020-11-27 15:43

    Another possible way to fix this would be to give each object in your List an ID, then use @Html.DropDownListFor(model => model.IDs) and populate an array which holds the IDs.

    0 讨论(0)
  • 2020-11-27 15:44

    Faced the same issue. Without for loop, it only posted the first element of the list. After iterating through for loop, it can keep full list and post successfully.

     @if (Model.MyList!= null)
        {
        for (int i = 0; i < Model.MyList.Count; i++)
          {
            @Html.HiddenFor(x => x.MyList[i])
          }
        }
    
    0 讨论(0)
  • 2020-11-27 15:46

    I've just come across this issue and solved it simply by doing the following:

    @for(int i = 0; i < Model.ToGroups.Length; i++)
    {
        @Html.HiddenFor(model => Model.ToGroups[i])
    }
    

    By using a for instead of a foreach the model binding will work correctly and pick up all of your hidden values in the list. Seems like the simplest way to solve this problem.

    0 讨论(0)
  • 2020-11-27 15:49

    I started digging through the source code for HiddenFor, and I think the roadblock you're seeing is that your complex object MyList is not implicitly convertible to type string, so the framework treats your Model value as null and renders the value attribute empty.

    0 讨论(0)
  • 2020-11-27 15:51

    I've just found out (after a couple of hours of trying to figure out why model values weren't going back to the controller) that hidden for should follow the EditorFor.

    Unless I am doing something else wrong this is what I found. I will not make the mistake again.

    In the context of a Model that contains a list of another class.

    This will NOT work:

            @{
                for (int i = 0; i < Model.Categories.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(modelItem => Model.Categories[i].Id)
                            @Html.HiddenFor(modelItem => Model.Categories[i].ProductCategoryId)
                            @Html.HiddenFor(modelItem => Model.Categories[i].CategoryName)                            
                            @Html.DisplayFor(modelItem => Model.Categories[i].CategoryName)                            
                        </td>
                        <td>
                            @Html.HiddenFor(modelItem => Model.Categories[i].DailyPurchaseLimit)                                                        
                            @Html.EditorFor(modelItem => Model.Categories[i].DailyPurchaseLimit)
                            @Html.ValidationMessageFor(modelItem => Model.Categories[i].DailyPurchaseLimit)
                        </td>
                        <td style="text-align: center">
                            @Html.HiddenFor(modelItem => Model.Categories[i].IsSelected)                            
                            @Html.EditorFor(modelItem => Model.Categories[i].IsSelected)
                        </td>
                    </tr>
                }
            }
    

    Where as this WILL......

                for (int i = 0; i < Model.Categories.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.HiddenFor(modelItem => Model.Categories[i].Id)
                            @Html.HiddenFor(modelItem => Model.Categories[i].ProductCategoryId)
                            @Html.HiddenFor(modelItem => Model.Categories[i].CategoryName)                            
                            @Html.DisplayFor(modelItem => Model.Categories[i].CategoryName)                            
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => Model.Categories[i].DailyPurchaseLimit)
                            @Html.HiddenFor(modelItem => Model.Categories[i].DailyPurchaseLimit)                            
                            @Html.ValidationMessageFor(modelItem => Model.Categories[i].DailyPurchaseLimit)
                        </td>
                        <td style="text-align: center">
                            @Html.EditorFor(modelItem => Model.Categories[i].IsSelected)
                            @Html.HiddenFor(modelItem => Model.Categories[i].IsSelected)                            
                        </td>
                    </tr>
                }
    
    0 讨论(0)
提交回复
热议问题