2011-08-12 26 views
2

一個Ajax登陸對話框,我使用Asp.Net MVC 3(剃刀)和jQuery/jQueryUI的創建對話框登錄框。 我對這些技術還很陌生,並且遇到了問題。創建具有MVC 3和jQueryUI的

登錄表單是一個局部視圖,我使用下面的jQuery代碼到其加載到頁:

$('#log-in').click(function() { 
     if (ServerModel.UserId == 0) {//User not logged in, open login dialog 
      $("<div></div>") 
      .addClass("dialog") 
      .addClass("form-dialog") 
      .attr("id", "login-dialog") 
      .appendTo("body") 
      .dialog({ 
       title: 'LOGIN', 
       close: function() { $(this).remove() }, 
       modal: true, 
       width: 323, 
       resizable: false 
      }) 
      .load(ActionUrls.LogOn); 
     } 
    }); 

ActionUrls.LogOn,具有用於在所述控制器的登錄操作方法的路徑。

登錄表單局部視圖看起來沿着這些線路:

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "login-dialog" })) 
{  
    //Login form fileds in here with submit button at the end 
} 

下面是控制器代碼:

[HttpGet] 
    public ActionResult LogOn() 
    { 
     return PartialView("_Logon"); 
    } 

    [HttpPost] 
    public ActionResult LogOn(LogOnModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      CustomSqlMembershipProvider provider = new CustomSqlMembershipProvider(); 

      if (provider.ValidateUser(model.UserName, Security.ComputeHash(model.Password))) 
      { 
       FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 
       return RedirectToAction("Index", "Home");      
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     // something failed, redisplay partial view with the model 
     return PartialView("_Logon", model); 
    } 

這一切工作正常,但我有問題,當用戶通過身份驗證時,可以在控制器代碼中看到。我嘗試使用RedirectToAction(「Index」,「Home」),這意味着頁面應該在登錄用戶和對話框關閉的情況下重新加載。

此刻

然而,僅僅在登錄對話框重新加載在它整個頁面的內容。我知道這可能是正常行爲,因爲我在登錄視圖中告訴窗體更新對話框的UpdateTargetId。

所以,問題是,我可以達到我想要的整個頁面重載的結果,如果是這樣,如何?

任何幫助和提示,將是非常讚賞。

/奧拉

回答

0

而不是使用一個表單參數,你可以用jquerys ajax方法,並在用戶名和密碼,並在AJAX方法的成功函數發送如果用戶認證成功redirect用戶索引頁。

注意索引頁應該還是有辦法來確定用戶進行身份驗證或沒有其他任何人都可以typein的URL。

您認爲如何?

+0

使用表單的好處是,在Asp.Net MVC中,它自己傳回各種驗證信息。如果你願意接受你的建議,你必須自己去做這件事嗎? –

+0

什麼樣的驗證? – Baz1nga

+0

FYI有很多jquery庫供您驗證爲您驗證..就像http://docs.jquery.com/Plugins/validation或更多詳細信息:http://zoomzum.com/useful-jquery-form-validation/ – Baz1nga

0

在您.load Ajax調用,返回的數據包含重定向信息。我不知道這是隻對JSON職位,但它是值得一試:

... 
.load(ActionUrls.LogOn, function(data) { 
    if (data.redirect) { 
     window.location.href = data.redirect; 
    else { 
     // other 
    } 
}); 

更新:

,你需要連接到回調的事件是一個提交登錄信息,而不是加載登錄頁面的信息。我問你鉤住頂部的錯誤Ajax調用,您需要含住一個是這一個:

@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "login-dialog" })) 
{  
    //Login form fileds in here with submit button at the end 
} 

不幸的是,我不知道如何掛鉤到該事件。本文可能有所幫助: How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId?

+0

不,似乎沒有工作。當我從對話框中發佈表單時,來自加載的回調甚至沒有命中。 –

+0

我給你的信息不好。我問你掛鉤到加載表單的ajax事件,而不是掛在提交表單的ajax事件上。 – rkw

0

我注意到這是一個老問題,但我希望我的回覆可能有助於某人。 對於類似的東西,我重定向到控制器操作中的'helper'頁面,其中包含(對於我來說)只有scipt標記中的一些JavaScript(或者您應該鏈接它當然)。 在這種情況下,這就足夠了:

window.location.href = "http://www.foo.com"; 

...我還發現並關閉該對話框有:

$('#login-dialog').dialog('close'); 

對我來說是很好的,但我也很欣賞更好的解決方案。