2010-08-31 101 views
0

所有我試圖做的是創建一個登錄控件,我想放在我的主頁。MVC部分登錄控制

香港專業教育學院創建了一個登錄用戶控制如下:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of MyWebsite.LogOnModel)" %> 
<% Using Html.BeginForm() %> 
    <%: Html.ValidationSummary(True, "Login was unsuccessful. Please correct the errors and try again.")%> 
    <div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(Function(m) m.UserName) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.TextBoxFor(Function(m) m.UserName) %> 
       <%: Html.ValidationMessageFor(Function(m) m.UserName) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.LabelFor(Function(m) m.Password) %> 
      </div> 
      <div class="editor-field"> 
       <%: Html.PasswordFor(Function(m) m.Password) %> 
       <%: Html.ValidationMessageFor(Function(m) m.Password) %> 
      </div> 

      <div class="editor-label"> 
       <%: Html.CheckBoxFor(Function(m) m.RememberMe) %> 
       <%: Html.LabelFor(Function(m) m.RememberMe) %> 
      </div> 
      <p> 
       <input type="submit" value="Log On" /> 
      </p> 

    </div> 
<% End Using %> 

我比家裏的Index.aspx頁面上呈現這樣的:

Html.RenderPartial("UsrCtlLogin") 

它正確地顯示了在主頁上。但我的問題是我如何將它連接到AccountController邏輯。即在點擊登錄時,我希望它激發LogOn http方法並驗證用戶(如果提供了無效詳細信息,則顯示無效消息),並且如果它們成功,則將它們重定向到頁面。

如何創建用戶控件和AccountController之間的鏈接?

在此先感謝

回答

3

默認情況下,表單將發佈到當前uri。因此,如果這是你的主頁,你可以在你的家控制器創建一個方法:

[HttpPost] 
public ActionResult Index() { 
    //authenticate 
    return View(); 
} 

但你真的不想混控制器職責。因此,將您的表單操作更改爲發佈到/ users/login或任何您想要在AccountController中使用的發佈方法。

using (Html.BeginForm("Login", "Account", FormMethod.Post))