5

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.

vincentw56
  • 545
  • 8
  • 19
  • Are you including the jquery-unobtrusive at the main view? – Fals May 22 '14 at 21:06
  • iso @{ Html.RenderPartial("TestDetail", Model); } try @Html.Partial("TestDetail", Model) – Dieter B May 22 '14 at 21:09
  • @Fals - Yes, I have jquery-unobtrusive. It works fine if I remove "Test testin" from "TestDetailPost(Test testin)". – vincentw56 May 22 '14 at 21:14
  • @Devcon2 - That doesn't make any difference. – vincentw56 May 22 '14 at 21:16
  • Probly you are getting some exception. Take a look at the Google Chrome Console to see if something went wrong. – Fals May 22 '14 at 21:20
  • @Fals - no errors at all. I've been using Firebug and there are no errors. – vincentw56 May 22 '14 at 21:56
  • So I found out something else about this, if I change the "Test testin" from "TestDetailPost(Test testin)" to something like "string testin" it works fine. It appears it only breaks if I pass the same object model that I received back out. Sounds like a bug to me. – vincentw56 May 23 '14 at 01:32

1 Answers1

12

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.

Community
  • 1
  • 1
Yogiraj
  • 1,952
  • 17
  • 29
  • 1
    Wow, that fixed it. I've never really ran into this problem. It is really weird and unusual. Thanks for the help and the great explanation! – vincentw56 May 23 '14 at 02:49
  • Wow, been fighting gremlins in the system for 3 hours before finding this. Ugh. Thank you for posting. – Scott Decker Oct 13 '16 at 23:16