2012-11-03 51 views
1

我一直在试图找出这一个互联网。我正在尝试添加一个jQuery对话框窗口,该窗口将调用一个操作来登录一个用户,然后在成功登录后,将用户重定向到他们的配置文件页面,否则保持对话框窗口打开并用相应的错误消息提示用户。到目前为止,登录部分似乎可以工作,但当操作返回时,它将用户保持在同一页面上。我需要做出什么改变才能确定一个成功的登录和适当的重定向?这里是我的代码:ajax成功登录后重定向

"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); 
    } 

回答

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); 
    } 
+0

我应该我的行动看起来像两个方案之间进行区分? – user1790300

+0

你需要使用ajax action/json return –

+0

从我上面的动作来判断,我应该返回这个,不管ModelState.Valid是true还是false?如果它是假的,我希望用户看到模式面板上有一些错误消息,让他们知道他们做错了什么,无效的用户/合格组合等,否则执行重定向。 – user1790300