【发布时间】:2014-02-06 18:53:10
【问题描述】:
我想在用户单击按钮执行操作时显示加载屏幕,因为该操作需要 10 秒才能运行。
我在我的家庭控制器中有一个简单的 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”在睡眠 1 秒后永远不会消失。因此,当它应该恢复正常时,屏幕仍然保持加载状态。
这是:
return View()
[HTTP] 行是否正常工作?或者是这条线的东西:
$.post(url, null, function () {
//$("#PID")[0].innerHTML = data;
$("#divLoading").hide();
});
在某个地方,它无法识别视图已返回,并且不应再显示 divLoading。
【问题讨论】:
-
你检查你的javascript函数是否在你想调用hide的地方执行
-
我在 return View() 行上放了一个断点,当我执行它时直接进入非活动的 div 屏幕。我不去javascript函数。
-
就在 hide();发出警报,看看它是否出现
标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0