【发布时间】:2018-11-01 09:04:40
【问题描述】:
我正在使用 jQuery Ajax。我想每 5 秒加载一次我的表的数据。这是我尝试过的,但它不起作用。它不会返回任何错误或结果。
public ActionResult Index()
{
return View();
}
public PartialViewResult _List()
{
List<Purchase> model = db.Purchases.ToList();
return PartialView("_List", model);
}
<div id="loadList"></div>
@section scripts{
<script>
$(document).ready(function () {
setInterval(function () {
$("#loadList").load("~/Views/Purchases/_List.cshtml");
}, 3000);
});
</script>
}
我想在#loadListdiv中加载的部分视图。
@model IEnumerable<ChocolateFactory.Data.Purchase>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.RefNo)</th>
<th>@Html.DisplayNameFor(model => model.Date)</th>
<th>@Html.DisplayNameFor(model => model.Amount)</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.RefNo)</td>
<td>@Html.DisplayFor(modelItem => item.Date)</td>
<td>@Html.DisplayFor(modelItem => item.Amount)</td>
</tr>
}
</table>
部分视图在:
~/Views/Purchases/_List.cshtml
【问题讨论】:
-
您必须传递返回视图的控制器方法的 url,而不是
.load()函数中的.cshtml文件名 - 例如.load("@Url.Action("_List", YourController"); -
@StephenMuecke 我也试过这个。但它不起作用。
#loadList未在索引视图中加载。 -
$("#loadList").load("@Url.Action("_List", YourController")工作得很好。您在浏览器控制台中遇到什么错误?而且我们不知道您的_List.cshtml视图是什么 -
我更新了问题。它不会返回任何错误。对不起我的英语兄弟。
标签: c# jquery asp.net-mvc html asp.net-mvc-partialview