2017-01-11 4 views
0

私は簡単な質問です。私はMVCビュー(Multiple Forms in same page ASP.net MVCのような)で複数のフォームを使用しようとしています - それぞれが同じコントローラ上で異なるアクションを呼び出します。私の問題は、ビューとは異なる名前のアクションを呼び出すフォームでエラーが発生することです。以下の簡単な例:MVCビューの複数のフォーム

Index.cshtml

@using (Html.BeginForm("index", null, FormMethod.Post, new { @class = "form -horizontal" })) 
{ 
    <button class="btn btn-primary" type="submit">index</button> 
} 
<br /> 
@using (Html.BeginForm("foobar", null, FormMethod.Post, new { @class = "form -horizontal" })) 
{ 
    <button class="btn btn-primary" type="submit">foobar</button> 
} 

testController

public class testController : Controller 
{ 
    // GET: test 
    public ActionResult Index() 
    { 
     return View(); 
    } 
    [HttpPost, ActionName("Index")] 
    public ActionResult IndexPost() 
    { 
     return View(); 
    } 
    [HttpPost] 
    public ActionResult foobar() 
    { 
     return View(); 
    } 

"インデックス" ボタンの作品をクリックする(何もしません) "foobarに" ボタンをクリックすると、エラーがスローされます

The view 'foobar' or its master was not found or no view engine supports the searched locations. 

私は明らかに何かが欠けている - すべての知恵は理解されて

私はこれが役立つだろう願ってい
+0

'ビュー(「インデックス」)を返す;'両方のPOSTメソッド –

+0

に 'リターンビュー()を使用する場合は、'コントローラが自動的に同じでビューを探します名前をアクションとして使用するので、おそらく存在しない 'foobar.cshtml'のためのviewsフォルダを探しています。前述のように、ビュー名パラメータを追加すると、 'foobar'アクションは代わりにインデックスビューを返します。 – Jake

+0

ありがとう!戻り値View( "Index")はこの例では機能します。これは明らかに簡単なテストです。実際のモデルベースのもので試してみましょう。 –

答えて

1

[HttpPost, ActionName("Index")] 
public ActionResult IndexPost() 
{ 
    return View("Index"); 
} 
[HttpPost] 
public ActionResult foobar() 
{ 
    return View("Index"); 
} 
関連する問題