1

我一直在互联网上试图弄清楚这一点。我正在尝试添加一个 jquery 对话窗口,该窗口将调用一个操作来登录用户,然后在成功登录后将用户重定向到他们的个人资料页面,否则保持对话窗口打开并用适当的错误消息提示用户。到目前为止,登录部分似乎可以工作,但是当操作返回时,它会使用户保持在同一页面上。我需要进行哪些更改才能确定成功登录并正确重定向?这是我的代码:

"Javascript code"
$.validator.unobtrusive.parse('#LogOnForm');
$('#LogOnDialog').dialog({
    autoOpen: false, width: 450, height: 300, modal: true,
     buttons: {
        'Log On': function () {
            if ($('#LogOnForm').validate().form()){
                $.ajax({
                    url: '@Url.Action("LogOnPartial", "Account")',
                        type: 'POST',
                        data: $('form').serialize(),
                        datatype: 'json',
                        success: function (result) {
                            $('#LogOnDialog').html(result).dialog('open');
                        }
                    });
                }
            },
            Cancel: function () { 
                $(this).dialog('close'); 
            }
        }
    });




    $('#linkSignIn').live('click', function () {
        $('#LogOnDialog').html('')
        .dialog('option', 'title', 'Sign In')
        .load('@Url.Action("LogOnPartial", "Account")', function () { $('#LogOnDialog').dialog('open'); });
    });



    "Controller Action"
    [HttpPost]
    public ActionResult LogOnPartial(LogOnModel model)
    {
        if (ModelState.IsValid)
        {
            UserPrincipal principal = new UserPrincipal(model.EmailAddress, model.Password);
            HttpContext.User = principal;
            FormsAuthentication.SetAuthCookie(model.EmailAddress, true);
            return PartialView("LogOnPartial", model);
        }

        return PartialView("LogOnPartial", model);
    }
4

1 回答 1

2

我不确定你想如何实现这一点,但在你的结果中你很可能想要返回你想要登录的配置文件的用户的 ID。

   success: function (result) {
                           if(result=='')/// no result show the dialog again 
                            {
                             $('#LogOnDialog').html(result).dialog('open');
                            }
                            else // redirect to profile page 
                            {
                                 window.location = 'profile/'+result;   
                            }
                    }
                });

你的行为可能是这样的

    public ActionResult ProvinceFilter(LogOnModel model)
    {
      string result=="";    
      UserPrincipal principal = new UserPrincipal(model.EmailAddress, model.Password); //in order to retorn exact error you must modify the principle to check if the user is valid or not and return specific error
     if(principal==null) //or not valid 
      {
         result="Your Username or Password is not correct";
      }
      else
       {
        HttpContext.User = principal;
        FormsAuthentication.SetAuthCookie(model.EmailAddress, true);
        result=principal.UserID.ToString();
       }
        return Json(result);
    }
于 2012-11-03T01:09:50.800 回答