登录弹出窗口在mvc成功的部分视图中加载整页?

时间:2016-04-29 11:18:34

标签: ajax asp.net-mvc forms asp.net-mvc-4 partial-views

我遇到一个问题,即我的登录弹出窗口中的ajax表单将主页加载到成功的部分视图中。当出现故障或验证问题时,将重新加载部分视图,但在登录成功时不应该这样。

Navbar中的Ajax表单:

<li class="login">
                    <a href="javascript:void(0);" tabindex="5"><span>Login</span></a>
                    @using (Ajax.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, new AjaxOptions
                    {
                        HttpMethod = "POST",
                        UpdateTargetId = "login_box"

                    }, new { id = "login_box" }))
                    {
                        @Html.Action("loginform", "Home")
                    }
                </li>

然后从共享的部分视图名称_loginpartial(缩短)加载表单的内容:

<div class="login_box_lower">
    <p class="login_box_or">or</p>
    <p class="login_sign_in">Sign in</p>
    <div style="position:relative;">
        @Html.EditorFor(m => m.Email, new { htmlAttributes = new { placeholder = "Email", maxlength = "30", @class = "login_username clearable" } })
        <span class="login_username_icon"></span>
    </div>
    <div style="position:relative;">
        @Html.EditorFor(m => m.Password, new { htmlAttributes = new { placeholder = "Password", maxlength = "18", @class = "login_pw clearable" } })
        <span class="login_pw_icon"></span>
    </div>

    <div class="rememberbox" >
        @Html.CheckBoxFor(m => m.RememberMe, new { @class = "remembercheck" })
        @Html.LabelFor(m => m.RememberMe)
    </div>

    <input type="submit" class="login_button" value="Log in" />

控制器操作:

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return PartialView("_LoginPartial", model);
        }

        // Require the user to have a confirmed email before they can log on.
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user != null)
        {
            if (!await UserManager.IsEmailConfirmedAsync(user.Id))
            {
                string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account-Resend");


                ViewBag.errorMessage = "You must have a confirmed email to log on. "
                                     + "The confirmation token has been resent to your email account.";
                return View("Error");
            }
        }


        // 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 PartialView("_LoginPartial", model);
        }
    }

RedirectToLocal操作符合默认操作:

private ActionResult RedirectToLocal(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }
        return RedirectToAction("Index", "Home");
    }

因为我看到如果登录成功并且returnUrl字符串为null,那么应该返回主索引视图?为什么只在登录弹出窗口的部分视图中返回?

此外,我在Ajax表单开头生成的returnURL字符串似乎不起作用,所以如果有人可以向我建议我哪里出错,那将非常有帮助。谢谢。

0 个答案:

没有答案