c# - MVC 4 中的等待屏幕

标签 c# asp.net-mvc asp.net-mvc-4 c#-4.0

我想在用户单击按钮执行操作时显示加载屏幕,因为该操作需要 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/

相关文章:

c# - 如何检测 C# Windows 窗体代码在 Visual Studio 中执行?

javascript - Html.ActionLink : popup(custom), 然后重定向

asp.net-mvc - 从具有常量的类中在 cshtml 上呈现字符串

asp.net-mvc-4 - MVC 4-Web Api和JSON?

c# - 从子窗体访问父窗体上的控件

c# - 为什么 chunk written length = 1023 而不是 1024?

asp.net - jquery 复选框

c# - 有没有办法通过数据注释来验证一个日期属性是否大于或等于另一个日期属性?

c# - 有没有办法缩写自定义类类型声明?

asp.net - 当多个用户从 asp 成员(member)身份登录时,如何将用户名放入 sql 触发器中