2010-08-31 97 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))