2011-04-12 55 views
1

嘿, 我是mvc.net新手。 我有一個主頁,它有一個登錄。現在我想能夠從任何頁面登錄。 但我似乎無法得到它的工作。我有一個AccountController與方法登錄和母版頁。 這是母版MVC C#登錄Masterpage

<% using (Html.BeginForm("LogOn", "Account")) 
       { %> 
      <%: Html.ValidationSummary(true, "Login foutief. probeer opnieuw.") %> 
      <div id="login_panel"> 

        <fieldset> 

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

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

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

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

         <div class="editor-label"> 
          <%: Html.CheckBoxFor(m => m.RememberMe) %> 
          <%: Html.LabelFor(m => m.RememberMe) %> 
         </div> 

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

      </div> 
      <% } %> 

登錄這是控制器

[HttpPost] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (persoonRepos.aanmelden(model.UserName, model.Password)) 
      { 
       FormsService.SignIn(model.UserName, model.RememberMe); 
       if (!String.IsNullOrEmpty(returnUrl)) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 
     return View(model); 
    } 

當我測試它,它不會去到accountmodel登錄方法。

有人能幫助我嗎?

+0

你在哪裏設置你的斷點時測試,看看它是否進入LogOn方法? – curtisk 2011-04-12 16:52:18

+0

我的斷點是在登錄方法中,它會引發這個問題。我想這行:Html.BeginForm(「LogOn」,「帳戶」)它應該這樣做。 – 2011-04-13 09:09:16

+0

你是否用global.asax中的路由改變了任何東西? – curtisk 2011-04-13 12:17:42

回答

0

根據你的意思,我能想到的唯一的事情就是保持這種形式不受LogOn控制器操作的影響,即母版頁不知道LogOnModel,然後無法正確傳遞給控制器。

在你的母版的最頂端,你將有這樣

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> 

線如果你有以上的母版直接嵌入形式,那麼就需要知道的LogOnModel的,所以使這種情況發生使該頂線看起來就像這樣......

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<YourProjectName.Models.LogOnModel>" %> 

當然替換「爲yourprojectname」與您的項目名稱...那麼,除非有別的東西我們不是您的設置看,那提交登錄時應該點擊LogOn功能。

+0

<%@ Master Language =「C#」Inherits =「System.Web.Mvc.ViewMasterPage 」%> 不能在代碼中使用此行;) – 2011-04-14 14:51:02