Why is my HiddenFor property having an empty value in the MVC4 view

When dealing with ASP.NET MVC you might use hidden fields to pass values of model properties from the view to an action of a controller. So you will find yourself writing something like that in your view:

@Html.HiddenFor(model => model.FirstName)

However, when you check the created HTML code with the F12 debugger, you see something like this:

<input id="FirstName" name="FirstName" type="hidden" value>

The reason for the value not being set is because you are posting data, with the HttpPostAttribute, to an MVC action and you are returning modified data within this action to your view, then the ModelState will contain only the values posted. For this reason, you should add the ModelState.CLear() line before returning the model to your view, so that the new values are shown.

A similar issue can be found in Stackoverflow.

comments powered by Disqus