我需要在我的一个MVC应用程序中实现自动刷新,并且我遇到了一些问题。 当我使用jquery.get()和部分视图完成自动刷新逻辑时,它在Chrome和Opera(最新版本)中完美运行,但在IE 9中不起作用。我花了大约一天的时间试图弄清楚问题,但无济于事。
我做了一个小测试应用程序,只是为了测试所有浏览器中的自动刷新功能。我对这个应用程序有同样的问题。
我已经附上了以下代码,如果有人能告诉我代码的问题,我将不胜感激?如果您需要更多相关信息,请与我们联系。
谢谢, ABHI。
index.cshtml
@model IEnumerable<AutoRefreshInMVC.Views.ViewModels.HomeIndexModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.7.2-vsdoc.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.20.min.js" type="text/javascript"></script>
<title>Index</title>
<script type="text/javascript">
function refresh() {
$.get('/Home/_Quotes', function (result) {
$('#refreshme').html(result);
});
}
$(document).ready(function () {
setInterval("refresh();", 10000);
});
</script>
</head>
<body>
<div id="refreshme">
@Html.Partial("_Quotes", Model)
</div>
的HomeController
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<HomeIndexModel> model = new GetQuotes().GetData();
return View(model);
}
private static List<HomeIndexModel> GetData()
{
List<HomeIndexModel> model = new GetQuotes().GetData();
return model.OrderByDescending(i => i.Change).ToList();
}
public ActionResult _Quotes()
{
List<HomeIndexModel> model = GetData();
return View(model);
}
}
_Quotes.chtml
@model IEnumerable<AutoRefreshInMVC.Views.ViewModels.HomeIndexModel>
<table id="tblIndex">
<tr id="header">
<th>
Code
</th>
<th>
LastValue
</th>
<th>
CurrentValue
</th>
<th>
Change
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastValue)
</td>
<td>
@Html.DisplayFor(modelItem => item.CurrentValue)
</td>
<td class="Change">
@Html.DisplayFor(modelItem => item.Change)
</td>
</tr>
}
答案 0 :(得分:2)
setInterval应如下所示:
setInterval(refresh(), 10000);
另外,请注意您的调用可能已缓存,因此您可能希望在_Quotes方法上放置一个OutputCache属性。
答案 1 :(得分:2)
我使用jquery简写$.get
和$.post
方法在这些方面遇到了一些问题,这些方法与缓存有关。
尝试将$.get
替换为具有禁用缓存的等效$.ajax
,即
$.ajax({
url: '/Home/_Quotes',
success: function (result) {
$('#refreshme').html(result);
},
cache: false
});