簡體   English   中英

如何在MVC中隱藏警報消息

[英]How to hide alert message in MVC

我是jQuery和MVC的新手。 我有一個登錄方案,當用戶名或密碼錯誤時顯示alert消息。 我使用ViewData將消息從控制器傳遞到視圖並通過javascript顯示警報消息。

單擊“登錄”按鈕后,會顯示“ UserName/Password is wrong. Please try again的警告消息UserName/Password is wrong. Please try again UserName/Password is wrong. Please try again按OK按鈕UserName/Password is wrong. Please try again 單擊“確定”按鈕將關閉彈出窗口。 當我按下注冊新用戶時,它會導航到注冊頁面,但當我按下返回按鈕進入登錄屏幕時,警告消息會再次顯示在登錄屏幕上。 以下是我正在使用的代碼。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        var result = _proxy.Get<LoginResult>(string.Format("{0}api/Login/Login?userName={1}&password={2}", ConfigurationUtils.GetValue("ApplicationServiceUri"), model.UserName, model.Password));

        if (result.Success)
        {
        }
        else
        {
            TempData["alertMessage"] = result.Message;
            return View();
        }
    }

    return View(model);
}

我的瀏覽腳本是:

$(document).ready(function () {             
    var success = @((TempData["alertMessage"] != null).ToString().ToLower());
    debugger;
    if (success == true) {
        var status = '@TempData["alertMessage"]';
        var wnd = $("#window");
        $(".spn-login-message").text(status);
        if (!wnd.data("kendoWindow")) {
            wnd.kendoWindow({
                modal: true,
                title: "Login",

                close: function () {
                    $(".open-button").show();
                },
                visible: false
            });
        }
        var dialog = $("#window").data("kendoWindow");
        dialog.center();
        wnd.data("kendoWindow").open();
        $(this).hide();

        $(".btn-primary").click(function () {
            // call 'close' method on nearest kendoWindow
            $(this).closest("[data-role=window]").kendoWindow("close");
        });
    }
    else{
        $("#btnOK").hide();

    }
});

我擔心的是當按下頁面刷新或后退按鈕導航到登錄頁面時如何隱藏警告消息。 提前致謝。 任何幫助將不勝感激。

我建議你使用這里描述Post-Redirect-Get模式

所以在你的情況下,你會這樣做:

 if (result.Success)
    {
    }
    else
    {
        TempData["alertMessage"] = result.Message;
        RedirectToAction("Login");
    }

請注意,我們正在重定向(將執行get),而不是返回視圖。 如果用戶刷新,那么他們將刷新get而不是post。

我已經完成了這種情況。 謝謝大家 :)

以下是我的代碼。

jQuery(document).ready(function($) {

            if (window.history && window.history.pushState) {

                $(window).on('popstate', function() {
                    var hashLocation = location.hash;
                    var hashSplit = hashLocation.split("#!/");
                    var hashName = hashSplit[1];

                    if (hashName !== '') {
                        var hash = window.location.hash;
                        if (hash === '') {

                        }
                    }
                });

                window.history.pushState('forward', null, './#forward');
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM