2011-03-09 44 views
3

我真的很困惑, 這裏是代碼:HTTPPost不起作用ASP MVC 3

[HttpPost] 
    public ActionResult Settings(string SubmitButton) 
    { 
     if (SubmitButton == "Sign In") { 
      ServiceLocator.Current.GetInstance<IAppContext>().LoggedUser = null; 
      Response.Cookies["loginuser"].Expires = DateTime.Now; 
      return RedirectToAction("Logon", "Account"); 
     } 
     if (SubmitButton == "Sign Up") { return RedirectToAction("register", "Account"); } 
     if (SubmitButton == "Change Default Ride Settings") { return RedirectToAction("changeSettings", "Home"); } 
     return View(); 
    } 

視圖包含

<% using (Html.BeginForm()) { %> 

    Three input , 

<% } %> 

控制器不與httppost解僱,但與HTTPGET

解僱

回答

2

您可能需要在您的視圖中傳遞Html.BeginForm()中的控制器和動作名稱。由於爲HTTP請求調用了[HttpPost] Settings()動作,這意味着沒有另一個用於獲取請求的Settings()動作,所以我猜測你的視圖是通過不同的動作提供的。在這種情況下,您需要在Html.BeginForm()中明確設置控制器和操作。試試這個:

<% using (Html.BeginForm("Settings", "YourControllerName")) { %> 
+0

沒問題,與jquerymobile,我修正了它與版本1.03 – Khaldoun 2011-03-09 19:46:55

+1

@ Khaldoun可以請你分享你的答案,我也使用JQUeryMobile和掙扎與類似的問題,在我的情況下LogOn方法RedirectToAction不工作和地址欄填充#標誌和重定向地址......無能爲力 – 2011-07-15 11:25:46

2

如果您想要發佈帖子,您必須生成一個html表單並將方法屬性設置爲發佈:

Html.BeginForm("action","controller", FormMethod.Post) { ... } 
+1

這也是我的第一個想法,但[無參數的BeginForm默認爲FormMethod.Post](http://aspnet.codeplex.com/SourceControl/changeset/view/63930#288009)。 – Rup 2011-03-09 15:05:29

0

應該有與名稱指數()和它不應方含任何參數的行動。這是我遇到的問題。

0

我用ActionName()來解決同樣的問題,

不工作代碼:

[HttpGet] 
    public ViewResult RsvpForm() 
    { 

    [HttpPost] 
     public ViewResult RsvpFrom() 
     { 
     } 

工作代碼:

[HttpGet] 
     public ViewResult RsvpForm() 
     { 
     } 
     [HttpPost, ActionName("RsvpForm")] 
     public ViewResult RsvpFromPost() 
     { 
     } 
0

使用剃刀

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post, new { id = "form1" })) 
{ 
    //form content 
} 
的正確方法