oglester
oglester

Reputation: 6655

Model not rendering model values after post

I have an ASP.NET MVC page that has a list of items...

//a vague representation
Model.someValue[0]     Model.someHiddenValue[0] 
Model.someValue[1]     Model.someHiddenValue[1]
Model.someValue[2]     Model.someHiddenValue[2]

All fields are optional, but they do have some validation, of which I am showing validation messages.

The problem is, when I submit once and return the original view instead of a redirect, the hidden fields are not getting their new value, but their new value is in the view model being passed to the view.

I am thinking the ModelState might be overriding the model as part of validation. I know I can do a redirect and bypass the problem, but I want be able to save part of the form and show validation errors for the rest. If there are no other solutions, I will simply validate the whole form and only save when all items are valid.

Upvotes: 0

Views: 461

Answers (2)

Brad Wilson
Brad Wilson

Reputation: 70626

If values are present in ModelState, it shows them instead of the values in your model, since they represent the "last" known state provided from the user.

You should clear out ModelState if you're not actually intending the user to re-do their edits.

Upvotes: 2

eu-ge-ne
eu-ge-ne

Reputation: 28153

Did you try this?:

var newValue = new ValueProviderResult("value", "value", System.Globalization.CultureInfo.CurrentUICulture);

ModelState.SetModelValue("someHiddenValue", newValue);

Upvotes: 0

Related Questions