Html.Hidden field not getting set

前端 未结 1 786
名媛妹妹
名媛妹妹 2021-01-19 03:59

I have a hidden field in my view like this:

using (Html.BeginForm(\"Action\", \"Schedule\"))
{
    @Html.Hidden(\"Id\", Model.Schedule.Id)
    ...
}
<         


        
相关标签:
1条回答
  • 2021-01-19 04:05

    Figured this out with the help of this question: MVC3 Model Binding - List to Hidden fields

    Apparently HTML helpers check ModelState for a value before they check Model. The reason why I only saw this behavior when I added the Id as a parameter to the action method was that this invoked the model binder to populate ModelState with the Id. And the reason why the Id was always an empty Guid was because that's the value the first time the action method is called.

    I added this line to my action method and everything works fine now:

    ModelState.Remove("Id")
    
    0 讨论(0)
提交回复
热议问题