Rajesh
Rajesh

Reputation: 2502

Hidden fields don't get updated after HTML form post back

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

The value for JobIndicator doesn't get refreshed after submitting the page.

I can see the value getting updated when I have it in the display field.

Upvotes: 29

Views: 13847

Answers (5)

Mike
Mike

Reputation: 629

Instead of having...

@Html.HiddenFor(m => m.JobIndicator)

if you enclose the variable in an object it will then work as expected.

@Html.HiddenFor(m => m.SomeOject.JobIndicator)

It Only seems to be that top level.

Upvotes: 0

Nalin Jayasuriya
Nalin Jayasuriya

Reputation: 377

Ran in to this issue myself. A solution (though not elegant) is to use the basic HTML syntax for this and use the model's value.

<input type="hidden" name="JobIndicator" value="@Model.JobIndicator">

Upvotes: 3

ocuenca
ocuenca

Reputation: 39326

The problem is Html helpers get data from ModelState and not from model you pass when you call post action.To solve this, you can call ModelState.Clear() in the post action before you return your view, this way the info in the ModelState is going to be cleared and repopulated once your view is regenerated.

You can find more info about this issue (and other solutions) in this blog

Upvotes: 35

Ahmed Ammar
Ahmed Ammar

Reputation: 1

Just make sure to put this line inside the form tag

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

Upvotes: -2

ThePaye
ThePaye

Reputation: 1611

Put this in your controller :

ModelState.Remove("JobIndicator");

Then your hidden field will be updated.

Upvotes: 44

Related Questions