asp.net mvc4 Ajax登陆:RedirectToAction不工作 [英] asp.net mvc4 ajax login: RedirectToAction not working

查看:1372
本文介绍了asp.net mvc4 Ajax登陆:RedirectToAction不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着做一个Ajax调用使用证书进入了一个jQuery UI的对话框登录。

AJAX调用正确的登录用户,并成功到达返回RedirectToAction(指数,家)的调用。然而,用户不会立即看到他们登录。我必须刷新页面的_LoginPartial状态栏,从注册/登录用户名/注销切换。

我觉得RedirectToAction(索引,家)调用将刷新页面。
我试着将它转换为RedirectToAction(索引,其他一些网页),虽然它贯穿指定的操作方法是不重定向到该页面。

下面是Ajax调用:

  $阿贾克斯({
                            网址:/帐号/登录,
                            键入:POST,
                            数据:JSON.stringify($('#登录表单)serializeObject()。)
                            数据类型:JSON,
                            的contentType:应用/ JSON的;字符集= UTF-8,
                            完成:功能(){
                                $('。dialogContainer)对话框(关闭)。
                            }
                        });

控制器:

  [HttpPost]
    公众的ActionResult登录(LoginModel型号,串RETURNURL)
    {        如果(ModelState.IsValid)
        {
            如果(Membership.ValidateUser(model.UserName,model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
                如果(Url.IsLocalUrl(RETURNURL))
                {
                    返回重定向(RETURNURL);
                }
                其他
                {
                    返回RedirectToAction(指数,家);
                }
            }
            其他
            {
                ModelState.AddModelError(,提供的用户名或密码不正确。);
            }
        }        //如果我们走到这一步,事情失败了,重新显示形式
        返回查看(模型);
    }


解决方案

做一个Ajax调用时RedirectToAction不起作用。出于同样的原因Ajax调用不会导致页面刷新,所以也没有你的RedirecToAction不会导致页面重定向。要重定向Ajax请求,而不是网页。

您将有某种成功的指标返回到你的Ajax调用,让你的Ajax调用重定向的页​​面。

Im trying to do an ajax call to login using credentials entered into a jquery ui dialog.

The ajax call properly logs the user and successfully reaches the return RedirectToAction("Index", "Home") call. However the user does not immediately see that they are logged in. I have to refresh the page for the _LoginPartial status bar to switch from Register/Login to Username/Logout.

I would think the RedirectToAction("Index", "Home") call would refresh the page. I tried to switch it to RedirectToAction("Index", "Some other page") and although it runs through the action method specified is does not redirect to that page.

Here is the ajax call:

          $.ajax({
                            url: "/Account/Login",
                            type: "POST",
                            data: JSON.stringify($('#loginForm').serializeObject()),
                            dataType: 'json',
                            contentType: "application/json; charset=utf-8",
                            complete: function () {
                                $('.dialogContainer').dialog("close");
                            }
                        });

controller:

    [HttpPost]
    public ActionResult Login(LoginModel model, string returnUrl)
    {

        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (Url.IsLocalUrl(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
        return View(model);
    }

解决方案

RedirectToAction does not work when doing an Ajax call. For the same reason the ajax call does not cause a page refresh, so too does your RedirecToAction not cause the page to redirect. You are redirecting the Ajax request, not the page.

You will have to return some kind of indicator of success to your ajax call and have your ajax call redirect the page.

这篇关于asp.net mvc4 Ajax登陆:RedirectToAction不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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