ASP.NET MVC5 Ajax Partial View加载整页

时间:2014-12-28 01:58:54

标签: ajax asp.net-mvc-5 asp.net-mvc-ajax

我正在使用ajax助手来异步加载数据,我正在使用MVC5,EF6并从控制器返回PartialView,但部分视图在整页上加载而不是异步更新DIV。以下是我的代码:

SampleDBContext db = new SampleDBContext();
public ActionResult Index()
{
    return View();
}

// Return all students
public PartialViewResult All()
{
    List<Student> model = db.Students.ToList();
    return PartialView("_Student", model);
}

我的index.cshtml代码是

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div style="font-family:Arial">

        <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
        <h2>Students</h2>

        @Ajax.ActionLink("All", "All",
    new AjaxOptions
    {
        HttpMethod = "GET", // HttpMethod to use, GET or POST
        UpdateTargetId = "divStudents", // ID of the HTML element to update
        InsertionMode = InsertionMode.Replace // Replace the existing contents
    })

        <br /><br />
        <div id="divStudents">
        </div>
    </div>

</body>
</html>

_Student.cshtml的标记部分视图是:

@model IEnumerable<GS.MVC_Ajax_Venkat3.Models.Student>
<table class="table" style="border:1px solid black; background-color:greenyellow">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TotalMarks)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TotalMarks)
            </td>
        </tr>
    }
</table>

我已将按键添加到网络配置中,如下所示

1 个答案:

答案 0 :(得分:0)

问题是我在布局页面和视图页面上两次引用了jquery.unobtrusive-ajax.js。我从View中删除了脚本引用,然后它工作正常。