为什么Partial View在MVC 5 Visual Studio 13中显示为完整页面?

Jes*_*rty 10 asp.net-mvc

我正在尝试使用以下内容在ASP.Net MVC 5(Visual Studio 13)中使用部分视图替换页面的一部分:

查看/预订/ Index.cshtml:

<div id="bargainBook">
    @Ajax.ActionLink("Click here for the Bargain Book!", 
    "BargainBook",
    new AjaxOptions
    {
        UpdateTargetId = "bargainBook",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET"
    })
</div>
Run Code Online (Sandbox Code Playgroud)

在BookController中:

public ActionResult BargainBook()
{
 var book = GetBargainBook();
 return PartialView("_BargainBook", book);
}

private Book GetBargainBook()
{
 return db.Books
     .OrderBy(b => b.Price)
     .First();
Run Code Online (Sandbox Code Playgroud)

}

在_BargainBook.cshtml中:

@model BookDemo.Models.Book

<div>
<p>
    <strong>Book</strong>
    @Model.Name
</p>
<p>
    <strong>Price</strong>
    @String.Format("{0:F}", @Model.Price)
</p>
</div>
Run Code Online (Sandbox Code Playgroud)

当我点击链接时,我会转到部分页面数据的完整页面视图.

Jes*_*rty 14

事实证明,默认情况下不包含jquery.unobtrusive-ajax.js.添加它解决了这个问题.


Ada*_*SFT 12

关于让这个工作的另一个注意事项.我希望VS 2013 RTM包含这个,但无论哪种方式,这都应该让你开始运行

  • 通过访问tools-library包管理器 - >包管理器控制台并输入,通过nuget安装jquery-validate
install-package Microsoft.jQuery.Ajax.Unobtrusive
  • 在/app_start/bundleconfig.cs中配置捆绑包
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
    "~/Scripts/jquery.unobtrusive*",
    "~/Scripts/jquery.validate*")
);
  • 将您的捆绑包添加到您的_layout.cshtml下方
@Scripts.Render("~/bundles/jquery")

或将其包含在您要使用它的每个视图中

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}