ASP.NET MVC登录模态的问题 - 重定向进入模态? [英] ASP.NET MVC Login Modal problems - redirect goes to modal?

查看:115
本文介绍了ASP.NET MVC登录模态的问题 - 重定向进入模态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经展示了在一个模式对话框(使用jQuery simplemodal)登录视图,当用户登录失败 - 它工作正常 - 错误追溯到模式。的问题是当用户成功登录 - 页面应该被重定向和模态消失,但是模态现在与整个页面:(

I have the login view showing up in a modal dialog (using jquery simplemodal), and when the user fails login - it works fine - the error goes back to the modal. The problem is when the user successfully logs in - the page is supposed to be redirected and the modal disappears, but the modal is now updated with the entire page :(

为了使模式看起来正确的 - 我检查了的AccountController,看是否登录的来源是阿贾克斯,如果是这样 - 返回一个局部视图(在模式显示),如果没有,返回完整视图(的情况下的用户试图从网站的外部访问一个页)

In order for the modal to look correct - i check to see in the AccountController if the source of the login was Ajax, if so - return a partial view (to display in the modal) if not, return the full view (in case a user tries to access a page from outside of the website)

public ActionResult LogOn()
    {
        if (Request.IsAjaxRequest())
        {
            return PartialView("_LogOn");
        }

        return View();
    }

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        if (Request.IsAjaxRequest())
        {
            return PartialView("_LogOn");
        }

        return View();
    }

在我登录用户控件(中的Site.Master一个多数民众赞成)我加了一个脚本JavaScript来登录点击登录链接时

In my Logon UserControl (the one thats in Site.Master) i added a script to logon from javascript when the login link is clicked

<script type="text/javascript">
    function openModel() {   
        $.ajax({
            type: "get",
            url: '/Account/LogOn',
            dataType: "html",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            //  async: true,  
            success: function (result) {
                $.modal(result, {
                    closeHTML: "",
                    containerId: "login-container",    
                    overlayClose: true
                });
            }
        });  

    }       
</script>

虽然局部视图(即大干快上的Ajax调用返回)看起来是这样的:

While the partial view (that gets returned on Ajax call) looks like this:

<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions{UpdateTargetId = "logonForm"})) { %>
<div id="logonForm">
    <%: Html.ValidationSummary(true, "Login was unsuccessful. Please review and try again.") %>

    <p>Please enter your username and password.
            </p>

        <p>
            <input id="submit" type="submit" value="Log In" />
        </p>
</div>

&LT;%}%>

<% } %>

所以,问题是,AjaxOptions UpdateTargetId是logonForm - 这是当用户登录失败我想要什么,但是当成功login..i希望它正常更新页面 - 但我不能登录阿贾克斯区分在AccountController.LogOn正常登录,除非我这样做:p

So the problem is that the AjaxOptions UpdateTargetId is the logonForm - which is what i want when the user fails to login, but when the successfully login..i want it to update the page normally - but i cant differentiate between ajax login and normal login in AccountController.LogOn unless i do this :P

任何想法?希望这是有道理的 - 感谢

Any ideas? Hope this makes sense - thanks

它像IM拧两种方式。

Its like im screwed either way.

推荐答案

因此​​,而不是总是返回HTML回成功JavaScript回调我已经推荐返回JSON只要您有一个返回RedirectToAction 返回重定向

So instead of always returning html back to the success javascript callback I'd recommended returning json wherever you have a return RedirectToAction or return Redirect.

if (Request.IsAjaxRequest())
    return Json(new {Url = returnUrl});
else 
    return Redirect(returnUrl);

或者

if (Request.IsAjaxRequest())
    return Json(new {Url = Url.Action("Index", "Home")});
else 
    return RedirectToAction("Index", "Home");

在JavaScript端做一些像

On the javascript side do something like

success: function (result) {
    if(typeof result === 'string'){
        $.modal(result, {
            closeHTML: "",
            containerId: "login-container",    
            overlayClose: true
         });
    }
    else if (result.Url){
        location.href = result.Url;
    }
}

这篇关于ASP.NET MVC登录模态的问题 - 重定向进入模态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆