clarence_odbody
clarence_odbody

Reputation: 185

Multiple forms on MVC view

I have what I hope is a simple question. I am trying to use multiple forms on an MVC view (like at Multiple Forms in same page ASP.net MVC) - each calling different actions on the same controller. My issue is that any form where I call an action that is named different from the view I get an error. Simple example below:

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();
    }

Clicking the "index" button works (does nothing) Clicking the "foobar" button throws an error

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

I am obviously missing something - any wisdom is appreciated

Upvotes: 0

Views: 1356

Answers (1)

Running Rabbit
Running Rabbit

Reputation: 2720

I hope this would help:

[HttpPost, ActionName("Index")]
public ActionResult IndexPost()
{
    return View("Index");
}
[HttpPost]
public ActionResult foobar()
{
    return View("Index");
}

Upvotes: 1

Related Questions