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

时间:2018-10-23 05:50:35

标签: c# viewbag

我正在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>

4 个答案:

答案 0 :(得分:3)

您可以使用JavaScript setTimeout函数运行代码以使用查询fadeOut函数隐藏消息:

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

并在视野范围内:

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

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

答案 1 :(得分:0)

您可以使用Java语言的 setTimeout()函数来实现此功能。

为标签提供一个特定的ID,例如“ lblMsg

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

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

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

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

答案 2 :(得分:0)

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

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

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

或使用CSS选择器display: none

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

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

答案 3 :(得分:-1)

为邮件的容器提供一个ID。然后添加15秒后隐藏容器的document.ready方法。使用settimeout函数,如下所示:

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