2009-10-12 82 views
3

我有一個名爲LogOn的局部視圖,其中我基本上將登錄輸入複製到控件中。我使用Html.RenderPartial放置在我的index.html控制的Ajax.BeginFormASP.NET MVC +模型狀態和局部視圖

<div id="login_ajaxtarget"> 
    <% using (Ajax.BeginForm("Logon", "Account", new AjaxOptions { UpdateTargetId = "login_ajaxtarget", HttpMethod = "Post" })) { %> 

     <% Html.RenderPartial("LogOn"); %> 

    <% } %> 
     </div> 

我試圖傳回的驗證消息的裏面,讓他們展示,但我似乎無法得到它的工作。我將模型傳遞給視圖,但它似乎並未正確顯示驗證。

我控制器

public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl) 
    { 

     if (!ValidateLogOn(userName, password)) 
     { 
      return PartialView("LogOn", ModelState); 
      //return RedirectToAction("Index", "Home"); 
     } 

     FormsAuth.SignIn(userName, rememberMe); 
     if (!String.IsNullOrEmpty(returnUrl)) 
     { 
      return Redirect(returnUrl); 
     } 
     else 
     { 
      return RedirectToAction("Index", "Home"); 
     } 
    } 

我的部分觀點

<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %> 


     <div> 
      <fieldset> 
       <legend>Account Information</legend> 
       <p> 
        <label for="username">Username:</label> 
        <%= Html.TextBox("username") %> 
        <%= Html.ValidationMessage("username") %> 
       </p> 
       <p> 
        <label for="password">Password:</label> 
        <%= Html.Password("password") %> 
        <%= Html.ValidationMessage("password") %> 
       </p> 
       <p> 
        <%= Html.CheckBox("rememberMe") %> <label class="inline" for="rememberMe">Remember me?</label> 
       </p> 
       <p> 
        <input type="submit" value="Log On" /> 
       </p> 
      </fieldset> 
     </div> 

我想使用的建議,從這個線程(http://forums.asp.net/p/1398814/3023892.aspx#3023892),但林不知道這是否是正確的。我真正想要的是能夠將LogOn功能放置在主頁上,而不必導航到新頁面才能使用它。如果有一個更簡單的方法來做到這一點,我所有的耳朵!提前致謝。

回答

0

我覺得有些東西是必需的。您沒有檢查輸入是否爲空,也沒有向模型狀態添加任何錯誤消息。

// In your action add somethings like bellow. 

if (String.IsNullOrEmpty(userName)) 
    this.ModelState.AddModelError("UserName", "The username is required."); 

if (this.ModelState.IsValid) 
{ 
    // Do login 
} 

return PartialView("Login"); 
+0

對不起,我添加的代碼是我迄今爲止改變的代碼。我已經實現了ValidateLogOn方法(或者更具體地說,它已經爲我實現了)。我只是試圖改變開始一個新的MVC項目時提供的開箱即用的網站模板。我的問題是,一旦錯誤被添加到模型中,它們就不會出現在我的頁面的驗證消息中。 – jimbo 2009-10-13 12:57:33

0

嘿男人真的很抱歉。我還沒有看到默認的MVCApplication項目模板。

檢查出來可能會解決你的問題。

控制:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script> 

<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script> 

<%= Html.ValidationSummary("Login was unsuccessful. Please correct the errors and try again.") %> 
<% using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions() { UpdateTargetId = "UpdatePanel" })) 
    { %> 
<div id="UpdatePanel"> 
    <fieldset> 
     <legend>Account Information</legend> 
     <p> 
      <label for="username"> 
       Username:</label> 
      <%= Html.TextBox("username") %> 
      <%= Html.ValidationMessage("username") %> 
     </p> 
     <p> 
      <label for="password"> 
       Password:</label> 
      <%= Html.Password("password") %> 
      <%= Html.ValidationMessage("password") %> 
     </p> 
     <p> 
      <%= Html.CheckBox("rememberMe") %> 
      <label class="inline" for="rememberMe"> 
       Remember me?</label> 
     </p> 
     <p> 
      <input type="submit" value="Log On" /> 
     </p> 
    </fieldset> 
</div> 
<% } %> 

代碼:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl) 
{ 

    if (!ValidateLogOn(userName, password)) 
    { 
     return PartialView("Login"); 
    } 

    FormsAuth.SignIn(userName, rememberMe); 
    if (!String.IsNullOrEmpty(returnUrl)) 
    { 
     return Redirect(returnUrl); 
    } 
    else 
    { 
     return RedirectToAction("Index", "Home"); 
    } 
} 

這對我的作品。如果問題仍然存在,請通過Firebug查看回復和請求。

希望這可以幫助