Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

@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.

like image 263
Rajesh Avatar asked Feb 22 '12 20:02

Rajesh


People also ask

Are hidden fields submitted?

A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

How can we keep hidden field value on postback in asp net?

If you refresh the page or click on a hyperlink that does a GET, then the value will be lost or revert to the designer-generated default. Back to your question, if you have a designer-generated HiddenField (in the aspx file), it should automatically set the value on postback.

How does HTML HiddenFor work?

HiddenFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, IDictionary<String,Object>) Returns an HTML hidden input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.

How do you clear fields after form submit in ASP NET MVC?

The Form Fields which are created using Model class are cleared by clearing the Model using ModelState. Clear function. int personId = person.


2 Answers

Put this in your controller :

ModelState.Remove("JobIndicator"); 

Then your hidden field will be updated.

like image 111
ThePaye Avatar answered Sep 22 '22 08:09

ThePaye


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

like image 37
octavioccl Avatar answered Sep 20 '22 08:09

octavioccl