ASP.Net MVC Html.HiddenFor with wrong value

That’s normal and it is how HTML helpers work. They first use the value of the POST request and after that the value in the model. This means that even if you modify the value of the model in your controller action if there is the same variable in the POST request your modification will be ignored and the POSTed value will be used.

One possible workaround is to remove this value from the model state in the controller action which is trying to modify the value:

// remove the Step variable from the model state 
// if you want the changes in the model to be
// taken into account
ModelState.Remove("Step");
model.Step = 2;

Another possibility is to write a custom HTML helper which will always use the value of the model and ignore POST values.

And yet another possibility:

<input type="hidden" name="Step" value="<%: Model.Step %>" />

Leave a Comment