.NET MVC Ajax Form preserves old input values ​​- how to update model value?

I have a properly working ajax form in an MVC3 application, with one exception. After I submit the form and get the result, even though the past in the model has a different input value (the sum in the example below), the old value is still displayed on the screen. I associate this with storing the value in the DOM and overriding / preventing a new value from the model. There are various ways to fix this by running the javascript function in one of the form events.

I would like to receive feedback on how best to deal with this situation, preferably native to MVC and without javascript. Here are the code snippets:

Parent view:

<div id="MyContainder"> @Html.Partial("MyPartialView", ClassContainingAmountProperty) </div> 

Partial view:

 @using (Ajax.BeginForm(actionName: "myaction", ajaxOptions: new AjaxOptions() UpdateTargetId = "MyContainder"}) @Html.EditorFor(x => x.Amount) <input type="submit" value="Save" /> ) 

controller

 myModel.Amount = SomeNewNumber; return PartialView(myModel); //same partial view returned but with new amount 
+7
source share
2 answers

As soon as I ask a question, I found the answer! ASP.NET MVC 3 Ajax.BeginForm and Html.TextBoxFor do not reflect changes made on the server

You have to call

 ModelState.Clear(); 

inside an action before returning a partial view

+21
source

Try to enter

 ModelState.Clear(); 
+5
source

All Articles