asp.net-mvc-4 – 如何在MVC 4中每3秒刷新一次局部视图?
|
我需要根据已完成的作业数量来更新进度条.完成的作业数存储在SQL Server DB的作业表中.我需要我的ASP.NET MVC 4应用程序每隔3秒查询数据库以查找竞争数量,并使用此数据更新部分视图中保存的进度条.计时器工作,并在StatusController中调用我的_getforStatus操作,它似乎调用EntityDB,但似乎永远不会在计时器告诉之前调用视图.如何让进度条每3秒刷新一次? 我的_Layout.cshtml视图的标题调用了StatusController中的计时器的启动,如下所示: <head>
...
@Html.Action("InitTimer",'Status")
...
</head>
此外,在_Layout视图中,我将局部视图称为Jumbotron,如下所示: <div class="jumbotron" id=progress">
@{Html.RenderAction("_GetforStatus","Status");}
</div>
Status控制器的编码如下: public class StatusController : Controller
{
IntegrationDBEntities _db;
Private Timer timer1;
Public void initTimer()
{
timer1 = new Timer();
timer1.elapsed += new ElapsedEventhandler(timer_Elapsed(;
timer1.interval = 3000;
timer1.Start();
}
Private void timer_Elapsed(Object sender,EventArgs e)
{
_GetforStatus();
}
[ChildActiononly]
public PartialViewResult _GetforStatus(0
{
_db = new IntegrationDBEntities();
ViewDataModel - _db.Jobs.ToList();
return partialView();
}
编辑: <script>
function loadPartialView() {
$.ajax({
url: '@url.Action("_GetforStatus',"Status")",type: 'GET',dataType: 'html',success: function(result) {
$('progress').html(result);
}
});
}
$function () {
loadPartialView();
window.setInterval("loadPartialView()",3000);
});
</script>
我不确定它是否不起作用bc我没有在JS中正确表示“Html.RenderAction”或者是什么. 解决方法我认为您需要对ajax调用进行一些更改.以下是我如何进行通话的示例$.ajax({
url: '@Url.Action("Action","Controller")',type: 'post',cache: false,async: true,data: { id: "frmUser" },success: function(result){
$('.divPartial').html(result);
}
});
在你的控制器上我不认为你只需要儿童行动.您还要返回一个空的局部视图对象.它应该是 return partialView('_partialName',Model);
终于在你的jquery中保持呼叫发生你需要重新触发呼叫.尝试这样的事情 $(document).ready(function(){
function RefreshPartial(){
//this will wait 3 seconds and then fire the load partial function
setTimeout(function(){
loadPartialView();
//recall this function so that it will continue to loop
RefreshPartial();
},3000);
}
//initialize the loop
RefreshPartial();
}); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-web-api – owin cors或web api cors
- asp.net下文件上传和文件删除的代码
- msbuild – 如果不指定目标框架,则不支持“发布”目标
- 为什么我的客户端去服务器来检查在使用ASP.NET MVC包时是否
- asp.net-mvc – 从FormCollection元素获取多个复选框
- asp.net-mvc – 运行Asp.Net Mvc作为OWIN中间件?
- asp.net 读取xml文件里面的内容,绑定到dropdownlist中
- asp.net – 程序或函数期望未提供的参数
- 运行SonarQube针对ASP.Net核心解决方案/项目
- asp.net – 形式auth超时和会话超时的差异
- asp.net – 使用sql server的Web应用程序中的审计
- asp.net-web-api – MVC Web API不能与Autofac集
- asp.net-mvc-4 – ASP.NET Web API授权令牌提前到
- 在ASP.NET Identity 2.0.0-beta1中禁用双因素身份
- ASP.NET隐藏字段与不可见的文本框
- asp.net-mvc – 谁设置HttpContext.User.Identit
- asp.net-core – CoreCLR中的哈希算法
- asp.net – JMeter测试ASP .NET网页有多好?
- .net – 为什么事件处理程序只能在IHttpModule初
- asp.net-mvc – 什么是防伪令牌盐的使用?
