ASP.NET MVC当作ajaxForm不能正常更新局部视图 [英] ASP.NET MVC AjaxForm not updating partial view properly
问题描述
我不知道我做错了。我以前从未有过这个问题,或者也许我有,但我从来没有注意到。我有一个局部视图的页面。当页面被提交时,该模型被检查,看它是否具有一个ID。如果这样做,更新记录。如果不是,它会创建一个新的。 pretty标准。一旦完成,该模型被返回到视图。我似乎具有的问题是,它没有被更新的任何改变模型。这只是被张贴相同的模型。好了,所以这里是一些code。我创建了一个全新的项目,它仍然无法正常工作。
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.
下面是控制器:
[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);
}
下面是测试的观点:
@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>
和模型:
public class Test
{
public string Id { get; set; }
public string Name { get; set; }
}
所以,我发现是,如果我从TestDetailPost动作去掉测试TESTIN,则返回我创建的模型。如果我不这样做,它只是返回被张贴相同的模型。当然,我没有做任何数据库保存或任何内容,code以上仅仅是试图找出为什么发生这种情况。
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.
下面是我在用的细节:
MVC5
jQuery的1.11.1
jquery.unobtrusive阿贾克斯
MVC5 jQuery 1.11.1 jquery.unobtrusive-ajax
我已经更新使用的NuGet到最新版本的所有文件。
I have updated all files to the latest version using NuGet.
推荐答案
呼叫 ModelState.Clear();
在你的操作方法如下图所示:
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);
}
我在<给定的更多详细信息href=\"http://stackoverflow.com/questions/13654879/mvc4-html-textboxfor-not-working-after-modifying-viewmodel/13661940#13661940\">my回答这里。我希望这有助于。
这篇关于ASP.NET MVC当作ajaxForm不能正常更新局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!