Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The model's Hidden bool field remains False after it was set True in the controller [duplicate]

I have this property in my ViewModel class:

public bool AreSimilarEntitiesChecked { get; set; }

In my controller I set its value 'true' and return the View with the model:

model.AreSimilarEntitiesChecked = true;

return View(model).WithWarning("Check the similar mentors before saving!");

On my View there is a form where I placed a hidden filed of this property:

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

After the View returned with the model that contains the AreSimilarEntitiesChecked its value remains False desipte the fact I set the True valuse in the controller.

I don't know what could be wrong with it.

Generated HTML:

<input name="AreSimilarEntitiesChecked" id="AreSimilarEntitiesChecked"
 type="hidden" value="False" data-val-required="The AreSimilarEntitiesChecked
 field is required." data-val="true">
like image 694
kmarton Avatar asked Feb 24 '15 11:02

kmarton


1 Answers

I can't be sure that this is the problem from your question, but I would bet a lot of money that it is...

MVC's ModelState, which keeps a representation of the view's model data, preferentially pulls values from the POST data rather than getting them from a bound model. That is, if the HTTP POST contained a field called (case-insensitively) AreSimilarEntitiesChecked with the value False, then it does not matter what you set that property to in the viewmodel when rendering a view. ModelState will prefer the POSTed value to the viewmodel value.

The reason for this odd behaviour is, let's say you have a field where a user is supposed to enter an integer and they write "banana" instead. This is sent to the server for validation, which it fails. We want to render the view again, with "banana" still in the field and a message that that isn't an integer. But if the view preferentially rendered the viewmodel's data, that wouldn't be possible, as "banana" isn't an integer and can't be put in that viewmodel field. Hence the POST values are retained.

There are two options for fixing this. Either you can fix it for this field specifically:

ModelState.Remove("AreSimilarEntitiesChecked");

Or the nuclear option:

ModelState.Clear();

More information on this behaviour here: http://weblog.west-wind.com/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes

like image 118
ChrisV Avatar answered Nov 02 '22 23:11

ChrisV