jquery - MVC5 Ajax 部分 View

标签 jquery ajax asp.net-mvc-5 partial-views unobtrusive-validation

我需要使用 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/

相关文章:

通过 Ajax 加载元素上的 Jquery

c# - 如何从保存在数据库中的字节数组设置CSS背景图像

asp.net-mvc - 如何在我的 MVC View 中使用 Bootstrap 按钮插件

javascript - 为什么类没有添加到我的元素中?

javascript - 如何引用被改变的当前元素?

javascript - 宽度不随 JS 变量改变

asp.net-mvc - MVC : Controller knows about persistence?

javascript - HTML5 数字旋转框控件不触发更改事件?

c# - 使用自定义 dll 时 AJAX 错误未返回 jqXHR.responseText.modelState

javascript - phonegap ajax 调用出现错误 '$ is not defined'