我想在用户单击按钮执行操作时显示加载屏幕,因为该操作需要 10 秒才能运行。
我在我的家庭 Controller 中有一个简单的 HTTP 帖子:
[HttpPost]
public ActionResult PostMethod()
{
System.Threading.Thread.Sleep(1000);
return View();
}
在我看来,我有:
@{
ViewBag.Title = "Home Page";
}
@section featured {
<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001; opacity: .8; filter: alpha(opacity=70);display:none">
<p style="position: absolute; top: 30%; left: 45%; color: White;">
Loading, please wait...<img src="../../Content/themes/base/images/ajax-loading.gif">
</p>
</div>
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
</div>
</section>
}
<button onclick="JavascriptFunction();">HTTPPost Button</button>
<script type="text/javascript" language="javascript">
function JavascriptFunction() {
var url = '@Url.Action("PostMethod", "Home")';
$("#divLoading").show();
$.post(url,function (res) {
$("#content-wrapper").html(res);
$("#divLoading").fadeOut(100);
});
}
</script>
基本上,当用户点击按钮时,Javascript 函数会显示“divLoading”div。这很好用。问题是“divLoading”在 sleep 1秒完成后永远不会消失。因此,当屏幕应该恢复正常时,屏幕就保持加载状态。
这是:
return View()
[HTTP] 线路工作正常吗?或者是这一行的内容:
$.post(url, null, function () {
//$("#PID")[0].innerHTML = data;
$("#divLoading").hide();
});
某处无法识别 View 已返回,并且不应再显示 divLoading。
最佳答案
刚刚意识到,如果您正在调用并执行 ajax 请求,您很可能不想返回 View ,请像这样思考
[HttpPost]
public ActionResult PostMethod()
{
System.Threading.Thread.Sleep(1000);
return Content("");
}
关于c# - MVC 4 中的等待屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611506/