我怎么能刷新我的网页每秒MVC中使用Ajax? [英] How can I refresh my page every second using Ajax in MVC?
本文介绍了我怎么能刷新我的网页每秒MVC中使用Ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要创造我的网页的机制,使得它刷新everysecond使用Ajax表数据。我该怎么做?
I want to create a mechanism in my page such that it refreshes the table data everysecond using Ajax. How can I do that?
推荐答案
假设你有一个控制器操作,将返回表,你可以使用局部视图:
Assuming that you have a controller action that will return the table as a partial view you can use:
<script type="text/javascript">
$(function() {
setInterval(loadTable,1000); // invoke load every second
loadTable(); // load on initial page loaded
});
function loadTable() {
$('#tablecontainer').load( '/controller/tabledata' );
}
</script>
<div id="tablecontainer">
</div>
您不得不您的资料表操作返回一个包含表的局部视图。
You'd have your tabledata action return a partial view containing the table.
<table>
<thead>...</thead>
<tbody>...</tbody>
</table>
修改通过单击处理程序,建立(script标签略)
EDIT to establish via a click handler (script tags omitted)
$(function() {
var timer;
$('#enableCheckbox').change( function() {
if ($(this).find(':checked').length) {
timer = setInterval(loadTable,1000); // set up timer
loadTable();
}
else if (timer) { // stop the interval timer
clearInterval(timer);
timer = null;
}
});
});
function loadTable() {
$('#tablecontainer').load( '/controller/tabledata #innercontainer' );
}
全视图(至少包含以下)
Full View (contains at least the following )
<div id="tablecontainer">
<div id="innercontainer">
<table>
<thead>...</thead>
<tbody>...</tbody>
</table>
</div>
</div>
这篇关于我怎么能刷新我的网页每秒MVC中使用Ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文