tags:

views:

145

answers:

2

Hello,

I have a single form with multiple submit buttons that have same value. Example: "Proceed".

Now, in ASP.Net MVC's post controller method, how do I know which button is pressed?

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult DepartmentDetails(string submitButton)

The value of submitButton will always be "Proceed". How do I know which button is pressed.

I have given separate IDs for each button.

Thanks.

+4  A: 

Try this:

<% using (Html.BeginForm())
   { %>
   <input type="submit" value="Submit" name="Submit1Button" />
   <input type="submit" value="Submit" name="Submit2Button" />
<%} %>

public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(FormCollection values)
    {
        string buttonName = values.AllKeys.Where((n) => n.EndsWith("Button")).SingleOrDefault();
        return View();
    }

}
Mehdi Golchin
Thanks. It works.
goths
+1  A: 

Would it not make more sense to break your page into two different forms?

You can then use the arguments of your Html.BeginForm HtmlHelper method to specify different Controller(s) Action methods for each form.

Mark Gibaud