2012-12-09 59 views
3

這個問題已被問了很多次,但我不認爲我需要它的方式。局部視圖登錄主頁

我試圖在主頁上實現登錄表單,但此表單位於彈出式部分,就像您在此網站上所示:http://myanimelist.net

我創建了一個局部視圖我的登錄表單:

@model ArtWebShop.Models.customers 

@section Validation { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

<section id="login"> 

    @using (Html.BeginForm("LoginValidate", "Login", FormMethod.Post, new { enctype = "multipart/form-data" })) 
    { 
     @Html.ValidationSummary(true) 
     @Html.ValidationSummary() 
     <fieldset> 
      <legend>Login</legend> 

      @Html.LabelFor(model => model.email) 
      @Html.TextBoxFor(m => m.email) 

      @Html.LabelFor(m => m.password) 
      @Html.PasswordFor(m => m.password) 

      <input type="submit" value="Login" class="button" /> 
     </fieldset> 
    } 

</section> 

這是在主頁(index.cshtml)所示

<section class="shown"> 
    @Html.Partial("_LoginPartial") 
    @Html.ValidationSummary(true) 
    @Html.ValidationSummary() 
</section> 

局部視圖顯示正確。 但現在來了不起作用的部分。

當你按登錄,如果你沒有填寫字段,驗證是在我的LoginController中完成,因爲它應該但我似乎無法將錯誤發送到我的主頁視圖時,我重定向。

[HttpPost] 
    public ActionResult LoginValidate(string redirect) 
    { 
     if (_unitOfWork.CustomersRepository.CostumerIsValid(Request.Form["email"], Request.Form["password"])) 
     { 
      if (!String.IsNullOrEmpty(redirect) || String.Compare(redirect, "none", StringComparison.OrdinalIgnoreCase) == 0) 
      { 
       if (redirect != null) Response.Redirect(redirect, true); 
      } 
      else 
      { 
       return RedirectToAction("index", "Home"); 
      } 
     } 
     ModelState.AddModelError("email", "You entered an incorrect email address"); 
     ModelState.AddModelError("password", "You entered an invalid password"); 

     return RedirectToAction("index", "Home"); 
    } 

這是我的loginValidation。由於沒有填充任何錯誤,所以將錯誤添加到ModelState中,然後重定向到主頁。但是,這並沒有顯示我的錯誤。我猜測這完全是因爲我重定向,但我該如何解決這個問題?

+0

如何打開登錄彈出窗口?你想和你提供的鏈接有相同的行爲嗎?爲什麼在出現錯誤時關閉彈出窗口?是不是更好的行爲像這裏:http://www.minidevils.de/minibreakdown/# –

+0

這確實是一個更好的例子。我使用我在OP中提供的鏈接來顯示我的意思是登錄彈出窗口,以防它不清楚。 –

回答

1

您可以使用TempDataDictionary在重定向之間存儲ModelStateDictionary

TempDataDictionary類
表示一組數據的持久存儲僅從一個請求到下一個。

在你LoginValidate行動,你將存儲的ModelState像這樣:

public ActionResult LoginValidate(string redirect) 
{ 
    /* Your other code here (omitted for better readability) */ 

    ModelState.AddModelError("email", "You entered an incorrect email address"); 
    ModelState.AddModelError("password", "You entered an invalid password"); 

    // Add the ModelState dictionary to TempData here. 
    TempData["ModelState"] = ModelState; 

    return RedirectToAction("index", "Home"); 
} 

而在你的HomeController指數行動,你會檢查TempData的ModelState

public ActionResult Index() 
{ 
    var modelState = TempData["ModelState"] as ModelStateDictionary; 
    if (modelState != null) 
    { 
     ModelState.Merge(modelState); 
    } 

    return View(); 
} 

您可以通過使用自定義操作過濾器使其更清潔一點;請參閱this blog,編號13.

+0

是的,那正是我需要的,謝謝!但有沒有辦法像重複視圖一樣通過重定向來傳遞錯誤信息(例如,返回視圖(「index」,model)?還是我只是想讓它變得太複雜而不是尋找其他方法)只是我想保持它儘可能乾淨,而不是混合太多不同的技術和模式。 –

+0

@reaper_unique你可能可以,但我不認爲它會更乾淨,我寧願去做一個ajax登錄並根據服務器的結果採取行動(可能是一個登錄好,重定向或錯誤),但這個答案只是解決它的一種方法=) –

1

您可以通過使用jQueryUI對話框和AjaxForms來實現此目的。請檢查這個演示:http://demo.ricardocovo.com/EditDialogsAndFormRazor/Cars。 您可以爲您的登錄表單執行類似於所提供鏈接中編輯功能的行爲。你會發現在這裏解釋的想法:http://ricardocovo.com/2012/04/06/asp-mvc3-editing-records-with-jqueryui-dialogs-and-ajaxforms-razor-version/

+0

這是我確保服務器驗證工作後,我會做的事情。 –

+0

您可以在操作方法中添加服務器驗證,當局部視圖表單將被取消時,我們將被異步調用 。 –