1

I've got a view with multiple submit buttons, and I don't know what the best way to handle this is. At the moment one of my controller actions looks like this:

[HttpPost]    
public ActionResult SaveAlbum(
    string saveButton,
    string createButton,
    string deleteButton,
    string showButton,
    string myHiddenValue)
{
    if (!String.IsNullOrEmpty(saveButton))
    {
        // do stuff
    }

    if (!String.IsNullOrEmpty(createButton))
    {
        // do stuff
    }

    // repeat for all buttons

    var viewModel = new MyViewModel(one, two, three);

    return View("Index", viewModel);
}

This looks pretty terrible. I'm assuming the best way would be to have a separate form for each submit button, which posts to different actions, but as it only posts what's inside the form and I need myHiddenValue for all of them I don't know what I should do. Should I use multiple hidden values instead? The hidden value is a comma separated list of values for all the checked checkboxes (added with jQuery).

Ingen Speciell
  • 107
  • 2
  • 13

3 Answers3

1

Ideally all your submits should go to individual Action methods on controller.

[Update] A quick search on stackoverflow gives following links:

Community
  • 1
  • 1
Darshan Joshi
  • 362
  • 1
  • 11
  • Yes, like I said that's what I assume but I don't know what to do when I need the hidden value for each of them. I updated the title of my question so it's clearer. – Ingen Speciell Jun 12 '14 at 08:31
  • Ideally, any submit action on the form should submit entire form. So you should get it. I don't have any working sample for the same though. – Darshan Joshi Jun 12 '14 at 08:38
0

It's easy

suppose you have two buttons

 <input type="button" name="btnSave"  value="Save"/>
 <input type="button" name="btnSave"  value="Update" />

your controller

  public ActionResult SaveAlbum(string btnSave)

if u click on Save, value of btnSave will be "Save" and if u click on Update button, value of btnSave at controller will be "Update". so give the save name to all your button but different values.

chandresh patel
  • 309
  • 1
  • 10
0
$('#btnUpdate').click(function(){
   e.preventDefault();
   $(form).attr("action", "Update");
   $(form).submit();
});

public ActionResult Update(string btnSave)

$('#btnSave').click(function(){
   e.preventDefault();
   $(form).attr("action", "Save");
   $(form).submit();
});

public ActionResult Save(string btnSave)
chandresh patel
  • 309
  • 1
  • 10