2012-02-03 84 views
0

我有一個新的asp.net MVC 3項目具有以下結構:ASP.NET MVC 3 - 兩種形式,兩個ActionResult但一個Controller?

瀏覽: /Home/Index.cshtml

@{ 
    Html.BeginForm("Index", "Home", "POST"); 
} 
<input id="name" type="text" /> 
<input id="submit_1" type="submit" value="submit" /> 
@{ 
    Html.EndForm(); 
} 

@{ 
    Html.BeginForm("FindTeacher", "Home", "POST"); 
} 
<input id="name" type="text" /> 
<input id="submit_2" type="submit" value="submit" /> 
@{ 
    Html.EndForm(); 
} 

控制器: /控制器/ HomeController的.cs

public class HomeController : Controller 
{ 

    [HttpPost] 
    public ActionResult Index(string name) 
    { 
     //call the model FindStudent() and set the ViewData 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult FindTeacher(string name) 
    { 
     //Call the FindTeacher() and set the ViewData 
     return View(); 
    } 
} 

的submit_1是作品,因爲它發現指數的ActionResult的,然而,當我點擊submit_2,它說找不到FindTeacher控制器。

所以,我該怎麼辦?

+1

'@using(Html.BeginForm(...)){...}' – SLaks 2012-02-03 06:21:50

回答

1

試試這個

[HttpPost] 
public ActionResult FindTeacher(string name) 
{ 
    // do updates 
    return RedirectToAction("Index"); or 
    return Index(); 
} 
+0

我設置返回RedirectToAction( 「指數」)斷點,它會發生。但我無法獲得[name]參數,它變爲空。 – Cheung 2012-02-03 07:16:22

+0

我使它工作,輸入應該有名稱屬性,不僅[id]像webform。 – Cheung 2012-02-03 07:17:57

1

"POST"是錯誤的;它應該是FormMethods.Post

正因爲如此,你的形式實際提交GET請求。

Index作品,因爲你有一個響應GET請求/Index不同的動作。
FindTeacher失敗,因爲沒有該行動沒有GET。