MVC C#重定向到登录模式

MVC C#重定向到登录模式,c#,jquery,asp.net-mvc,modal-dialog,C#,Jquery,Asp.net Mvc,Modal Dialog,我在#myModal正文中有一个登录表单。登录功能正常,但如果输入了错误的用户名和密码,则不会重定向到带有相应错误消息的#myModal。而是重定向到默认的登录页面。我不知道如何重定向到模式而不是默认的登录页面 模态 <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="doc

我在
#myModal
正文中有一个登录
表单
。登录功能正常,但如果输入了错误的用户名和密码,则不会重定向到带有相应错误消息的
#myModal
。而是重定向到默认的
登录
页面。我不知道如何重定向到
模式
而不是默认的
登录
页面

模态

<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>

&时代;
情态标题
@使用(Html.BeginForm(“Login”,“Account”,new{ReturnUrl=ViewBag.ReturnUrl},FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
使用本地帐户登录。

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Label(“电子邮件”,新的{@class=“col-md-2控制标签”}) @TextBox(“email”,“”,new{@class=“form control”,@placeholder=“email”}) @Label(“密码”,新的{@class=“col-md-2控制标签”}) @Html.Password(“Password”,“”,new{@class=“form control”,@placeholder=“Password”}) } 接近

登录控制器方法

    [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?
        }
    }
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginView模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
//如果modelstate无效,如何重定向回登录模式?
返回视图(模型);
}
//这不会将登录失败计入帐户锁定
//要使密码失败触发帐户锁定,请更改为shouldLockout:true
var result=wait SignInManager.PasswordSignInAsync(model.Email、model.Password、model.RememberMe、shouldllockout:false);
开关(结果)
{
案例标志状态成功:
返回重定向到本地(returnUrl);
案例标志状态锁定输出:
返回视图(“锁定”);
案例标志状态。要求验证:
return RedirectToAction(“SendCode”,new{ReturnUrl=ReturnUrl,RememberMe=model.RememberMe});
案例信号状态故障:
违约:
AddModelError(“,”登录尝试无效“);
返回视图(模型);
//如何重定向回登录模式?
}
}

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

您可以使用ajax完成此操作