2013-04-08 53 views
2

我使用登錄表單創建了一個網站。首先,我創建登錄表單,只是爲了在帳戶/登錄下進行測試。然後有些需要更改所有頁面應該顯示登錄。所以,我創建一個部分,並在_Layout.cshtml以下打電話,從部分視圖獲得價值mvc剃刀

<div id="logindisplay"> 
    @{ Html.RenderPartial("_LogOnPartial"); } 
</div> 

_LogOnPartial.cshtml:

@model HOP2013.Models.LogOnModel 

@{ 
    ViewBag.Title = "Log On"; 
} 


@if(Request.IsAuthenticated) { 
<text>Welcome <b>@Context.User.Identity.Name</b>! 
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text> 
    } 
    else { 
     @: @Html.ActionLink("Log On", "LogOn", "Account") 
    } 
      @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") 

    @using (Html.BeginForm()) { 

    <fieldset> 
     <legend>Account Information</legend> 
     <div id="user" class="user"> 
     <div class="editor-label"> 
      @Html.LabelFor(m => m.UserName) 
     </div> 
     <div class="editor-field"> 
      @Html.TextBoxFor(m => m.UserName, new { @title = "UserName" }) 
      @Html.ValidationMessageFor(m => m.UserName) 
     </div> 
     </div> 
     <div id="password" class="password"> 
     <div class="editor-label"> 
      @Html.LabelFor(m => m.Password) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.Password, new { @title = "UserName" }) 
      @Html.ValidationMessageFor(m => m.Password) 
     </div> 
     <div class="editor-label"> 
      @Html.CheckBoxFor(m => m.RememberMe) 
      @Html.LabelFor(m => m.RememberMe) 
     </div> 
     </div> 

     <p> 
      <input type="submit" class="login" value="Log On" /> 
     </p> 

     <p>New @Html.ActionLink("Register", "SignUp", "Account")Here</p> 
    </fieldset> 

} 

但我不能登錄。如果我使用單獨的頁面(LogOn.cshtml)登錄意味着它正在成功記錄。我不知道它爲什麼會發生。任何人都會澄清我。

回答

4

您需要明確指定表單中的控制器動作:

@using (Html.BeginForm("LogOn", "Account")) { 
    ... 
} 

的原因是因爲當你使用Html.BeginForm()助手不帶任何參數,它使用當前的URL爲形式的行動。但目前的網址可能完全不同,因爲這個視圖可以從任何控制器提供。通過明確指定要提交表單的控制器操作,助手將生成適當的操作屬性:

<form action="/Account/LogOn" method="post"> 
    ... 
</form>