繁体   English   中英

15 秒后在 C# 中自动隐藏 ViewBag 消息

[英]Auto-Hide ViewBag Message in C# after 15 seconds

我正在通过 C# 中的 ViewBag 显示一条消息,它工作正常。 现在我想要的是在 15 秒后自动隐藏 ViewBag 消息。 我该怎么做?

以下是我使用 ViewBag 的代码:

public ActionResult ForgetPassword(String EmailId, string message)
    {
        ViewBag.Message = message;
        ForgetPassword objForgetPassword = new ForgetPassword { EmailId = EmailId };
        return View();
    }

//我传递消息的其他模型

 if (objResult.Status)
            {
                return RedirectToAction("Login", new { message = "Password changed Successfully. Please Login with the New Password."});
            }

相同的 CSHTML 代码:

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal for-pass">
    <h4><b>@ViewBag.Message</b></h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.OTP, htmlAttributes: new { @class = "control-label" })
        <div class="col-md-12">
            @Html.EditorFor(model => model.OTP, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.OTP, "", new { @class = "text-danger" })
        </div>
    </div>

您可以使用 javascript setTimeout函数运行代码以使用查询淡出函数隐藏消息:

$(document).ready(function(){
    setTimeout(function(){
        $("#msg").fadeOut();
    }, 15000);
});

并认为:

<h4 id="msg"><b>@ViewBag.Message</b></h4>

这是小提琴: https : //dotnetfiddle.net/3Sw1O9

您可以使用 Javascript setTimeout()函数来执行此操作。

给你的标签一个特定的 Id,比如“ lblMsg

setTimeout(function(){ document.getElementById('lblMsg').style.display = "none"; }, 15000);

或者只是您想在 15 秒后删除元素。

使用 JQuery,您可以使用remove()函数。

setTimeout(function(){ $('#lblMsg').remove(); }, 15000);

您可以使用 JS 隐藏包含ViewBag数据的元素,如下所示:

setTimeout(function() { $("h4").hide(); }, 15000);

$("h4").delay(15000).fadeOut();

或者使用 CSS 选择器display: none

setTimeout(function() { $("h4").css('display','none'); }, 15000);

确保h4是唯一一个包含消息的元素,否则使用带有id属性的<div>

为消息的容器提供一个 id。 然后添加 document.ready 方法,在 15 秒后隐藏容器。 使用 settimeout 函数如下:

setTimeout(function() {
        $("#successMessage").hide('bind', {}, 500)
    }, 5000);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM