我正在使用 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>
我已将密钥添加到 web 配置中,如下所示