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>

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

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

您可以使用 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>

您可以使用javascriptsetTimeout function to run the code to hide the message using the query fadeOut函数:

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

并且在视图中:

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

这里是 fiddle:https://dotnetfiddle.net/3Sw1O9