在Razor页面中使用JavaScript进行简单的自动刷新
我想知道如何在不重新加载整个页面的情况下对HTML表进行简单的刷新 我想定时器设置为每5秒刷新一次 代码是:在Razor页面中使用JavaScript进行简单的自动刷新,javascript,jquery,asp.net-mvc,razor,Javascript,Jquery,Asp.net Mvc,Razor,我想知道如何在不重新加载整个页面的情况下对HTML表进行简单的刷新 我想定时器设置为每5秒刷新一次 代码是: <table class="table trim-list search-results" id="res"> <tbody> @foreach (Record record in Model.Results) { var renditionuri = getRendition(record);
<table class="table trim-list search-results" id="res">
<tbody>
@foreach (Record record in Model.Results)
{
var renditionuri = getRendition(record);
<tr style="float:left;">
<td class="row-icon">
<div id="thumb_col" style="margin-right:10%;">
<img class="thumbnail" style="padding:3px;" src="~/record/@record.Uri/recordrendition/@renditionuri"/>
</div>
</td>
<td class="prop-val">
<label style="font-weight:bold;">Title: </label> 
<a href="~/Record/@record.Uri">@record.Title</a> <br>
<label style="font-weight:bold;">Record Number:</label> 
@record.Number<br>
<label style="font-weight:bold;">Media Format:</label> 
@record.GetPropertyOrFieldString("MediaFormat")<br>
<label style="font-weight:bold;">Author:</label> 
@record.GetPropertyOrFieldString("RecordAuthor")<br />
<label style="font-weight:bold;">Tags:</label> 
@record.Notes
</td>
</tr>
}
}
</tbody>
</table>
@foreach(记录模型中的记录。结果)
{
var renditionuri=getRendition(记录);
标题:&thinsp;
&thinsp;
记录编号:&thinsp;
@记录编号
媒体格式:&thinsp;
@record.GetPropertyOrFieldString(“MediaFormat”)
作者:&thinsp;
@record.GetPropertyOrFieldString(“RecordAuthor”)
标签:&thinsp;
@记录,笔记
}
}
我不知道您是否尝试过任何方法,但您可以使用javascript和一些局部视图查看setInterval
以重新加载表
@model YourProject.Models.Results //or whatever your model is
<table class="table trim-list search-results" id="res">
<tbody>
@foreach (Record record in Model.Results)
{
var renditionuri = getRendition(record);
<tr style="float:left;">
<td class="row-icon">
<div id="thumb_col" style="margin-right:10%;">
<img class="thumbnail" style="padding:3px;" src="~/record/@record.Uri/recordrendition/@renditionuri"/>
</div>
</td>
<td class="prop-val">
<label style="font-weight:bold;">Title: </label> 
<a href="~/Record/@record.Uri">@record.Title</a> <br>
<label style="font-weight:bold;">Record Number:</label> 
@record.Number<br>
<label style="font-weight:bold;">Media Format:</label> 
@record.GetPropertyOrFieldString("MediaFormat")<br>
<label style="font-weight:bold;">Author:</label> 
@record.GetPropertyOrFieldString("RecordAuthor")<br />
<label style="font-weight:bold;">Tags:</label> 
@record.Notes
</td>
</tr>
}
}
</tbody>
</table>
<script>
$(function(){
setInterval(function(){
//this code will execute after page loads every 5 seconds, you could change on a click event or whatever you need
var url = "@Html.Raw(Url.Action("GetPartialResuls", "YourController", new { param1 = "-param1" }))";
//This part is only neccesary if you are using querystring parameters, if not you can ignore it.
url = url.replace("-param1", yourVariableOrValueHere);
//your partialview will reload every 5 seconds with the method you are calling in the controller
$("#tableResults").load(url);
}, 5000);
});
</script>
$(函数(){
setInterval(函数(){
//此代码将在页面加载后每5秒执行一次,您可以在单击事件或任何需要的情况下进行更改
var url=“@Html.Raw(url.Action(“getpartialresults”,“YourController”,new{param1=“-param1”}”);
//只有在使用querystring参数时才需要此部分,否则可以忽略它。
url=url.replace(“-param1”,yourVariableOrValueHere);
//您的partialview将使用您在控制器中调用的方法每5秒重新加载一次
$(“#tableResults”).load(url);
}, 5000);
});
我想这就是你需要的一切。希望有帮助。您可以使用信号器。请查看更多详细信息您尝试过什么吗?只是为了指出设置间隔是危险的。当请求超过5秒时会发生什么情况在请求完成后使用递归调用设置超时将更安全。
<div id="tableResults">
@{Html.RenderAction("GetPartialResults", "YourController");}
<!--you could pass parameters if you need them: , new{ param1 = "yourvalue" } after the controller -->
</div>
<script>
$(function(){
setInterval(function(){
//this code will execute after page loads every 5 seconds, you could change on a click event or whatever you need
var url = "@Html.Raw(Url.Action("GetPartialResuls", "YourController", new { param1 = "-param1" }))";
//This part is only neccesary if you are using querystring parameters, if not you can ignore it.
url = url.replace("-param1", yourVariableOrValueHere);
//your partialview will reload every 5 seconds with the method you are calling in the controller
$("#tableResults").load(url);
}, 5000);
});
</script>