ASP.NET MVC AjaxForm not updating partial view properly

asp.net-mvc, asp.net-mvc-4

Solution

Call `ModelState.Clear();` in your action method like below:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TestDetailPost(Test testin)
{
   ModelState.Clear();
   Test test = new Test();
   test.Id = "1";
   test.Name = "Guy";

   return this.PartialView("TestDetail", test);
}

I have given more details in my answer here. I hope this helps.

Problem

I'm not sure what I am doing wrong. I have never had this problem before or maybe I have, but I've never noticed. I have a page with a partial view. When the page is submitted, the model is checked to see if it has an ID. If it does, it updates the record. If not, it creates a new one. Pretty standard. Once it is done, the model is returned back to the view. The problem I seem to be having is that it isn't updated with any changes to the model. It is just the same model that was posted. Okay so here is some code. I created a brand new project and it still doesn't work. Also, I used Firebug to look at the raw data coming back and it is still the same model. Here is the controller: ``` [AcceptVerbs(HttpVerbs.Get)] public ActionResult Test() { return this.View(new Test()); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult TestDetailPost(Test testin) { Test test = new Test(); test.Id = "1"; test.Name = "Guy"; return this.PartialView("TestDetail", test); } ``` Here is the "Test" view: ``` @model WebAppTest.Models.Test @using (Ajax.BeginForm("TestDetailPost", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "TestDetail" })) { <p><input type="submit"/></p> <div id="TestDetail"> @{ Html.RenderPartial("TestDetail", Model); } </div> } ``` Here is the "Test Detail" view: ``` @model WebAppTest.Models.Test <p>@Html.TextBoxFor(a => a.Id)</p> <p>@Html.TextBoxFor(a => a.Name)</p> ``` And the model: ``` public class Test { public string Id { get; set; } public string Name { get; set; } } ``` So what I have found is that if I remove the "Test testin" from the TestDetailPost action, it returns the model I created. If I don't, it just returns the same model that was posted. Of course, I am not doing any DB saves or anything, the code above is just for trying to figure out why this is happening. Here is the details of what I am using: MVC5 jQuery 1.11.1 jquery.unobtrusive-ajax I have updated all files to the latest version using NuGet.

Original source

Related problems