微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在ASP.NET MVC警报中单击“确定”后显示时间

如何解决如何在ASP.NET MVC警报中单击“确定”后显示时间

我正在使用View.Bag显示“弹出窗口”,但是问题是当我单击“确定”时,时间消失了。即使关闭了弹出窗口,如何让时间继续? 时间必须持续,甚至关闭弹出窗口。

感谢任何帮助我的人。我为我的英语感到抱歉。

我的代码

Index.cshtml

@model RecuperarConta.Controllers.User
@using Newtonsoft.Json

@{
    ViewBag.Title = "Home Page";
}

<html>
<head>
    <script>
        
        // functions used to decrease time
        // if time runs out,a method is called that changes the user's state to true
        // and update the page so that a message appears saying that the link is no longer available
        function startCountdown(timeLeft) {

            var interval = setInterval(countdown,1000);

            update();

            function countdown() {

                if (--timeLeft > 0) {
                    update();
                } else {
                    clearInterval(interval);
                    update();
                    completed();
                }
            }

            function update() {
                hours = Math.floor(timeLeft / 3600);
                minutes = Math.floor((timeLeft % 3600) / 60);
                seconds = timeLeft % 60;
                //shows time in page
                document.getElementById('time-left').innerHTML = '' + minutes + ':' + seconds;
            }

            function completed() {

                $.ajax({
                    type: 'POST',url: 'Home/MyAction',data: {},success: function (response) {
                        window.location.href = response.redirectToUrl;
                    }
                });
            }
        }
    </script>
</head>
<body onload="startCountdown(600);">

    <p style="text-align:right;margin-right:100px;font-size:20px">Redirect in <span id="time-left"></span></p>


    @using (Html.BeginForm("Index","Home",FormMethod.Post,new { enctype = "multipart/form-data" }))
    {

        @Html.AntiForgeryToken();


        @*@Html.HiddenFor(model => model.email)*@

        <div class="form-group" style="text-align:center">
            <h4>Please enter a new password</h4><br />
            @*@Html.LabelFor(model => model.password,htmlAttributes: new { @class = "control-label col-md-2" })*@
            <h5>Enter Password</h5>

            @*@Html.EditorFor(model => model.password,new { htmlAttributes = new { @id = "password",placeholder = "Password",Type = "password",pattern = "/d{7}",title = "Password must be at least 7 characters" } })*@
            @Html.EditorFor(model => model.password,Type = "password" } })


            @Html.ValidationMessageFor(model => model.password,"",new { @class = "text-danger" })
        </div>

        <div class="form-group" style="text-align:center">
            @*@Html.LabelFor(model => model.password1,htmlAttributes: new { @class = "control-label col-md-2" })*@
            <h5>Confirm Password</h5>
            @Html.EditorFor(model => model.password1,new { htmlAttributes = new { @id = "password1",Type = "password" } })
            @Html.ValidationMessageFor(model => model.password1,new { @class = "text-danger" })

        </div>

        <br />
        <div class="form-group" style="text-align:center">
            <input type="submit" id="btn_Update" value="Change Password" class="btn btn-default" />
        </div>


        <div class="modal" id="popup">
            <div class="modal-dialog">
            </div>
            <div class="modal-body">
                <p>asa asfasfas</p>
            </div>
            <div class=" modal-footer">
                <div class="form-group" style="text-align:center">
                    <input type="submit" value="OK" class="btn btn-default" />
                </div>
            </div>
        </div>

    }

    @* will show the pop up if the passwords are not the same *@

    @if (ViewBag.Message != null)
    {
        <script type="text/javascript">
            window.onload = function () {
              //show message
               alert("@ViewBag.Message");

            };

        </script>
    }


     @*< !--jQuery library-- >*@
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script src="js/bootstrap.js"></script>

    </body>
</html>

控制器

    //method called when the "Change password" button is pressed
    [NoCache]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index([Bind(Include = "email,password,password1")] User user)
    {
        user.uid = "";

        User user1 = TempData["user"] as User;

        if (user1 != null)
            user.email = user1.email;

        
        // if passwords are different
        // then show a pop up
        if (user.password != user.password1)
        {
             ViewBag.Message = "Passwords do not match";
             return View(user);
        }

        //if the fields are empty
        if (user.password == null || user.password1 == null)
        {
            ViewBag.Message = "Empty fields";
            return View(user);
        }

    }

在弹出窗口出现之前

image

弹出窗口出现并关闭

image

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。