Updating value html
I added a plain text value to demonstrate the model value compared to what's rendered in the textbox.
The relevant markup is the email address which needs to be manipulated via the model in the Controller code.
Here's the Controller logic block that deals with that: The logic is straight forward - if the new email address is not valid because it already exists I don't want to display the new email address the user entered, but rather the old one.
To do this I change the value on the model which effectively does this: So when I press the Save button after entering in my new email address ([email protected]) here's what comes back in the rendered view: Notice that the textbox value and the raw displayed model value are different.
, you should be careful not to use it with untrusted model values, because that might open the possibility of a script injection attack.
NET MVC that I didn't really get until today: Html Helpers controls (like .
After getting quite a few comments on this post I quickly realized that the behavior I described above is actually the behavior you'd want in 99% of the binding scenarios.If it's not I want to reset the email address back to the old value which exists in the database and was previously store.The obvious thing to do would be to modify the model.So, you can also clear out individual values on an as needed basis: This allows you to remove a single value from the Model State and effectively allows you to replace that value for display from the model.While researching this I came across a post from Microsoft's Brad Wilson who describes the default binding behavior best in a forum post: The reason we use the posted value for editors rather than the model value is that the model may not be able to contain the value that the user typed.
Each Model State entry contains a value property that holds Attempted Value and Raw Value properties. On a POST operation however, it'll always used the Attempted Value to display the control.