MVC C#重定向到登录模式

时间:2017-02-16 16:48:57

标签: c# jquery asp.net-mvc modal-dialog

我在form正文中有一个登录#myModal。登录功能很好,除非输入了错误的用户名和密码,它不会重定向到带有相应错误消息的#myModal。而是重定向到默认的login页面。我不知道如何重定向到modal而不是默认的login页面。

模态

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <div class="modal-body">
            <section id="loginForm">
                @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    <h4>Use a local account to log in.</h4>
                    <hr />
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.Label("E-mail", new { @class = "col-md-2 control-label" })
                        <div class="col-md-10">
                            @Html.TextBox("email", "", new { @class = "form-control", @placeholder = "Email" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.Label("Password", new { @class = "col-md-2 control-label" })
                        <div class="col-md-10">
                            @Html.Password("Password", "", new { @class = "form-control", @placeholder = "Password" })
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Log in" class="btn btn-success" />
                        </div>
                    </div>
                }
            </section>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</div>

登录控制器方法

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            //how do I redirect back to the login modal if modelstate is invalid?
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
                //how do I redirect back to the login modal?
        }
    }

1 个答案:

答案 0 :(得分:0)

很遗憾,您无法重定向模式。模态是页面/视图的一部分。即使您单击模式内的登录按钮,该按钮也会执行POST,这将导致整个页面的重定向。您需要通过ajax对登录控制器进行POST,或者重定向到您的应用程序流程。