views:

215

answers:

4

Hello, How do I have 2 buttons and each button to perform a different post back action? For example :

  1. I want button1 to submit the contents of the form.
  2. I want button2 to display some data from the database without storing whats in the form.

Right now,since im using form collection,both buttons seem to store the data.How do I differentiate between the buttons?

Thanks

+1  A: 

Buttons do not store anything. Buttons initiate HTTP POST request.

Now if you wish those POSTs to hit different controller actions, you need to put them into two diferent forms and specify two different form urls.

<% BeginForm ("/action1"); %>
    <input type="submit" value="Action1" />
<% EndForm ();

<% BeginForm ("/action2"); %>
    <input type="submit" value="Action2" />
<% EndForm ();

Then you need to map those routes to two different controller actions (in your Global.asax):

routes.MapRoute(
    "Action1",
    "/action1",
    new { controller = "Test", action = "PostAction1" });

routes.MapRoute(
    "Action2",
    "/action2",
    new { controller = "Test", action = "PostAction2" });

And your controller is there waiting for those actions:

public class TestController : Controller
{
    [AcceptVerbs (HttpVerbs.Post)]
    public ActionResult PostAction1 (FormCollection form)
    {
        // Do something
        return View ();
    }


    [AcceptVerbs (HttpVerbs.Post)]
    public ActionResult PostAction2 (FormCollection form)
    {
        // Do something
        return View ();
    }
}

Then you decide yourself whatever you wish to happen on those actions, save data or do something else.

Developer Art
+2  A: 

You can put each button in a different form and have different actions handle that submit. Like this:

<% using (Html.BeginForm("/FirstAction")) {%>
   <input type="submit" />
<% } %>

<% using (Html.BeginForm("/SecondAction")) {%>
   <input type="submit" />
<% } %>
Robban
+2  A: 

You need to check for the existence of the relevant Submit button in the Form collection and do the relevant action.

e.g assuming Button1 & Button2 and you only want one form

in controller action method that accepts POST

public actionresult SomeMethod(FormCollection form)
{
  if (form.AllKeys.Contains("Button1")
  {
    doSomething();
  }
  else // assuming only two submit buttons
  {
    doSomethingElse();
  }
}

HTH ,

Dan

Daniel Elliott
+2  A: 

The two form approach is fine as long as the forms don't need to share input fields (textboxes etc.). If they need to share, or the buttons are to be positioned adjacent to each other (such that the forms overlap in some way) then using two forms begins to break down.

Also, I'm less keen on dispatching within the controller, based on detecting which button has been clicked from the FormCollection. The dispatching method produces a layer of indirection which makes things less clear: you cannot decorate the methods to which you dispatch with AcceptVerbs, for instance.

Instead, we simply catch the JavaScript onclick event and set the form's action.

<% using (Html.BeginForm("Save", "Contact"))
{ %>
    <input type="submit" value="Save" />
    <input type="submit" value="Find Similar" onclick="this.form.action = rootDir + 'Contact/Find'" />
<% }%>
Andy Grout