I am working with jQuery Ajax. I want to load data of my table every 5 seconds. Here's what I tried but it's not working. It doesn't return any error or result.
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>
}
The Partial View which I want to load in #loadList div.
@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>
The partial View is in:
~/Views/Purchases/_List.cshtml
.cshtmlfile name in the.load()function - e.g..load("@Url.Action("_List", YourController");#loadListis not loading in Index view.$("#loadList").load("@Url.Action("_List", YourController")works just fine. What errors are you getting in the browser console? And we do not know what your_List.cshtmlview is