对于有关Ajax.BeginForm的问题,有很多关于SOT的主题没有使用返回部分视图正确更新目标元素:
mvc4 ajax updating same page
ASP.NET MVC 4 – Ajax.BeginForm and html5
MVC 4 (razor) – Controller is returning a partialview but entire page is being updated
MVC 4 Ajax is not updating the PartialView within the page
但是,所有这些都可以通过手动编写jQuery ajax或者包含一个缺少的javascript文件来解答.
mvc4 ajax updating same page
ASP.NET MVC 4 – Ajax.BeginForm and html5
MVC 4 (razor) – Controller is returning a partialview but entire page is being updated
MVC 4 Ajax is not updating the PartialView within the page
但是,所有这些都可以通过手动编写jQuery ajax或者包含一个缺少的javascript文件来解答.
@using (Ajax.BeginForm("PostcardDetails",new AjaxOptions() { InsertionMode = InsertionMode.Replace,UpdateTargetId = "details" })) { <div id="PostcardSearchResults"> @{Html.RenderAction("PostcardSearchResults",Model);} </div> } <div id="details"> </div>
相关控制器代码:
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] public ActionResult PostcardSearchResults(PostcardSearchFilter filter) { PostcardSearchResults model = new PostcardSearchResults(filter); return PartialView("_PostcardSearchResults",model); }
在我的布局中,我引用了这些jQuery文件.此外,我已经验证了该页面正在输出正确的路径,并找到正确的文件.我尝试切换unobtrusive-ajax.min.js和validate.min.js的顺序,没有成功.
<script type="text/javascript" src="@Url.Content("~/Scripts/globalize.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.9.1.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.10.0.custom.min.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
另外,在我的网站的根web.config和我的View文件夹中的web.config我已经包括:
<add key="webpages:Version" value="2.0.0.0"/> <add key="PreserveLoginUrl" value="true"/> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
我在其他地方看不出来了.没有引发JavaScript错误,并且控制器被正确打击,返回了一个PartialViewResult. HTML中的Form元素正在填充所有正确的数据属性.