2011-04-10 115 views
2

我想學習MVC 3 &剃刀,我在這裏呆了3個小時了。這是我的MVC3在一個視圖中的兩個部分視圖

MVC項目使用默認模板創建使用帳戶註冊和模板中的所有好東西。我想要做的是在HomeController的索引中同時註冊頁面和登錄頁面,所以我爲Register(_RegisterPartial)和LogOn(_LogOnPartial)創建了一個局部視圖。當我進入索引頁面時,我看到註冊和登錄表單很好,但是當我嘗試登錄或註冊時,它進入無限循環。

我的HomeController看起來像這樣;

// ************************************** 
// Registration 
// ************************************** 

public ActionResult DoRegister() 
{ 
    return PartialView("_Register"); 
} 

[HttpPost] 
public ActionResult DoRegister(RegisterModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // Attempt to register the user 
     MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email, model.UserProfile); 

     if (createStatus == MembershipCreateStatus.Success) 
     { 
      FormsService.SignIn(model.UserName, false); // createPersistentCookie 
      return View("Success"); 
     } 
     else 
     { 
      ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus)); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    ViewBag.PasswordLength = MembershipService.MinPasswordLength; 
    return View(model); 
} 


// ************************************** 
// Login 
// ************************************** 

public ActionResult DoLogin() 
{ 
    return PartialView("_Login"); 
} 

[HttpPost] 
public ActionResult DoLogin(LogOnModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     if (MembershipService.ValidateUser(model.UserName, model.Password)) 
     { 
      // logged in 
      FormsService.SignIn(model.UserName, model.RememberMe); 
      if (Url.IsLocalUrl(returnUrl)) 
      { 
       Redirect(returnUrl); 
      } 
      else 
      { 
       View("Success"); 
      } 
     } 
     else 
     { 
      // Not logged in 
      ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    return View("Success"); 
} 

和我的cshtml看起來像這樣;

@{ 
    ViewBag.Title = "Home Page"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@if (Request.IsAuthenticated) 
{ 
    @Html.ActionLink("Log Off", "LogOff", "Account") 
} 
else 
{ 
    Html.RenderAction("DoLogin"); 
    Html.RenderAction("DoRegister"); 
} 

問候,

瑞安

回答

1

我不能完全複製你的情況,但我會說你的部分表單不能正確發佈。查看頁面的呈現html並檢查表單的發佈位置。我的猜測是他們被張貼到Index行動。再加上重定向,我認爲這就是無限循環的來源。

我的猜測是這兩個表單呈現的html都是相似的,併發布到相同的動作,即<form action="/" method="post">,因爲它們由HomeController的Index操作呈現。

變化的部分的形式(_Login.cshtml_Register.cshtml)和明確聲明張貼到(more on Html.BeginForm從MSDN),其動作/控制器組合

@using (Html.BeginForm("DoLogin","Home")) {/*snipped*/} //in _Login.cshtml 
@using (Html.BeginForm("DoRegister","Home")) {/*snipped*/} //in _Register.cshtml 

此外,我將在Html.RenderAction呼叫變爲

Html.RenderPartial("_Login"); 
Html.RenderPartial("_Register"); 
+0

Im來自ASP.NET webforms背景,我想了解如何將部分視圖的事件連接到代碼。由於在MVC中我們不能有代碼,我該如何告訴按鈕執行什麼動作。因爲如果我做Html.RenderPartial(「_ Login」);它呈現得很好,但當我點擊登錄按鈕時,沒有任何反應。我是否必須將登錄例程寫入呈現該局部視圖的每個控制器? – 2011-04-12 15:17:57

+1

@ryan - 上面放置的'Html.BeginForm'表示表單需要發送到哪個控制器/操作。一般來說,我認爲登錄表單和註冊表單總是會被髮布到同一個控制器/操作組合中,在這種情況下,「主頁」控制器的「DoLogin」和「DoRegistration」操作。我認爲你可能需要用部分視圖的代碼更新你的問題。 – Ahmad 2011-04-12 17:33:00

+0

我下班回家時會試試這個。非常感謝你 – 2011-04-12 20:56:46

3

你閱讀異常消息?

A public action method 'Register' was not found on controller 'AudioRage.Controllers.HomeController' 

現在看看你發佈的HomeController的代碼。你看到一個註冊行動嗎?我不。

所以加一個:

public ActionResult Register() 
{ 
    ... 
} 

在你HomeController有一個名爲註冊一個動作,但動作是通過POST動詞才能訪問,因爲它是裝飾用[HttpPost]屬性:

[HttpPost] 
[ActionName("Register")] 
public ActionResult Index(RegisterModel model) 

所以你不能用/Home/Register上的GET動詞來調用它。

+0

我做。他使用了ActionNameAttribute。 – Linkgoron 2011-04-11 08:26:37

+0

@Linkgoron,是的,他也使用了[HttpPost]屬性。 – 2011-04-11 08:30:26

+0

這是正確的。他的問題是理解get和post屬性,這與您剛纔陳述的不同。 – Linkgoron 2011-04-11 08:31:16