I am trying to refresh a partial view div in MVC 5 so a table will show new SQL data. However, I'm running into a problem. The way it is now, the div is not refreshing to include new data when I add any new data to my SQL table after page-load...just the data that was in the table on page-load.
Here is my Controller:
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);
}
Here is my Partial View:
@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>
The Index View:
@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);
});
And my Model:
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; }
}