javascript 在 MVC 5 中刷新部分视图 Div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31082741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh Partial View Div in MVC 5
提问by Erik
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.
我正在尝试刷新 MVC 5 中的部分视图 div,以便表将显示新的 SQL 数据。但是,我遇到了问题。现在的方式是,当我在页面加载后向 SQL 表添加任何新数据时,div 不会刷新以包含新数据......只是页面加载时表中的数据。
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; }
}
回答by
Your .load()
function is attempting to call a static file which will by default throw a 403 (Forbidden) error and no data is updated (I strongly suggest you learn to use your browser tools)
您的.load()
函数正在尝试调用一个静态文件,该文件默认会抛出 403(禁止)错误并且没有数据更新(我强烈建议您学习使用浏览器工具)
You need to create a controller method that generates your model and returns a partial view of your data. For example
您需要创建一个控制器方法来生成模型并返回数据的部分视图。例如
public ActionResult Fetch()
{
Notifications model = new Notifications();
.... // populate your model from the repository
return PartialView("_Notifications", model);
}
_Notifications.cshtml
_Notifications.cshtml
@model inConcert.Models.Notification
@foreach (var item in Model.Update)
{
<tr>
<td>@item.notificationMessage</td>
<td>@item.TimeStamp</td>
</tr>
}
In the main view, to initially load it, you can use
在主视图中,要初始加载它,您可以使用
@{Html.RenderAction("Fetch");}
which means you do not have to create and pass the model in the Index()
method
这意味着您不必在Index()
方法中创建和传递模型
Then in the script
然后在脚本中
var url = '@Url.Action("Fetch")';
var notifications = $("#notificationsTable"); // cache it to avoid repeatedly searching the DOM
setInterval(function () {
notifications.load(url);
}, 2000);
Side note: Unless you expecting the data to be constantly changing every 2 seconds, this approach could be very inefficient. As an aletrnative, you should consider using SignalRso your server-side code pushs content to the connected clients as it happens, in real-time. Refer also this tutorial.
旁注:除非您期望数据每 2 秒不断变化,否则这种方法可能非常低效。作为替代方案,您应该考虑使用SignalR,以便您的服务器端代码实时将内容推送到连接的客户端。另请参阅本教程。