Я пытаюсь обновить частичный div в MVC 5, чтобы таблица отображала новые данные SQL. Однако я столкнулся с проблемой. Как сейчас, div не освежает включение новых данных, когда я добавляю новые данные в таблицу SQL после загрузки страницы ... только данные, которые были в таблице на странице-загрузке.Обновить Partial View Div в MVC 5
Вот мой контроллер:
public ActionResult Index()
{
List<List<object>> result = DataAccess.DataAccess.Read(Build.StringArray("Notifications"));
Notifications Notify = new Notifications();
Notify.Update = new List<Notification>();
foreach (List<object> row in result)
{
Notification x = new Notification();
x.notificationMessage = (string)row[1];
x.ID = (int)row[0];
x.TimeStamp = (DateTime)row[2];
Notify.Update.Insert(0,x);
}
return View("Index",Notify);
}
Вот мой Частичный вид:
@model inConcert.Models.Notifications
<table class="table">
<tr>
<th>
<h3>Update</h3>
</th>
<th>
<h3>TimeStamp</h3>
</th>
</tr>
@foreach (var item in Model.Update)
{
<tr>
<td>
@item.notificationMessage
</td>
<td>
@item.TimeStamp
</td>
</tr>
}
</table>
Индекс Вид:
@model inConcert.Models.Notifications
<head>
<title></title>
<link href="@Url.Content("~/Content/Notifications.css")" rel="stylesheet" type="text/css" />
</head>
<div id="notificationsTable">
@Html.Partial("~/Views/Shared/NotificationPartial.cshtml")
</div>
<script type="text/javascript" src="~/Scripts/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
$("#notificationsTable").load("~/Views/Shared/NotificationPartial.cshtml");
}, 2000);
});
И моя модель:
public class Notification
{
[Key]
public int ID { get; set; }
public string notificationMessage { get; set; }
public DateTime TimeStamp { get; set; }
}
public class Notifications : DbContext
{
public List<Notification> Update { get; set; }
}
Функция 'load()' вызывает статический файл. Вам нужно вызвать метод контроллера, который генерирует данные и возвращает частичный вид на основе этих данных. –