我需要使用 Ajax.BeginForm 刷新带有 PartialView 的 div。 我已经在 MVC4 中这样做了十几次,而且效果非常完美。在 MVC5 中,它不起作用:(
以下是我采取的步骤:
- 在 Visual Studio 2013 中创建一个新项目“PartialAjax”(ASP.NET Web 应用程序/MVC)
- 右键单击“Views/Home”并添加 View > 选中“创建为部分 View ”> 将其命名为“_Test”
此 _Test.cshtml View 的代码:
<p>From now on I'm gonna change my life: @DateTime.Now.ToString()</p>
编辑 Views/Home/Index.cshtml View :
@{ ViewBag.Title = "Home Page"; } @using (Ajax.BeginForm("ChangeLife", "Home", null, new AjaxOptions() { UpdateTargetId = "test", HttpMethod = "Post" }, null)) { <input type="submit" value="Start" /> } <div id="test"> @Html.Partial("_Test") </div>
将其放入您的 HomeController 中:
public ActionResult ChangeLife() { return this.PartialView("_Test"); }
如果我单击“管理 NuGet 包”,则默认安装 jQuery 和 Microsoft jQuery Unobtrusive Validation。
最佳答案
我必须安装 Microsoft jQuery Unobtrusive Ajax 并呈现 jqueryval 的 javascript 包(已存在但未呈现),并为 unobtrusive.ajax 脚本文件添加了一个新包。
BundleConfig 类中的新 bundle :
bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
"~/Scripts/jquery.unobtrusive*"));
在 _Layout.cshtml 中为两个包添加了渲染方法(在 jquery 渲染下方):
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryajax")
关于jquery - MVC5 Ajax 部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25052694/