4

I have read many articles explaining how to handle 2 submit buttons in a form but none of them have worked for me.

form:

<form id="myForm">
<input type="submit" name="btngroup" value="remove" />
<input type="submit" name="btngroup" value="activate" />
</form>

js:

$('#myForm').submit(function (e) {
    e.preventDefault();
    var form = $(this);
    $.ajax({
            url: '/Admin/removeDocPermanently',
            type: 'POST',
            DataType: "html",
            data: form.serialize()
    });
});

controller

[HttpPost]
public ActionResult removeDocPermanently(string btngroup, FormCollection form)
{  
   //btngroup does not get the value of the clicked submit button
}

Is this possible to get the pressed submit button in the js submit function or the controller?

Kaf
  • 33,101
  • 7
  • 58
  • 78
Guilherme Longo
  • 2,278
  • 7
  • 44
  • 64
  • Possible duplicate of [Two submit buttons in one form](http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form) – mishmash Jan 15 '13 at 10:53

1 Answers1

3

You can hold the submit object on submit button click and use it in submit funciton.

var submitCommand;
$('input:submit').click(function(){
 submitCommand = $(this);
});


$('#myForm').submit(function (e) {
       alert(submitCommand.val());
       //your code
});
Adil
  • 146,340
  • 25
  • 209
  • 204
  • 1
    a bit hacky but likely the only thing to work... since js is assumed there are probably better ways than using 2 submits in the first place – Esailija Jan 15 '13 at 10:56