One option is to have separate shapes for each button. This is often impossible or easy without Javascript or garbled HTML. This makes it easy to send each request to a different action method.
This is a solution that I developed for a few image submit buttons. I am not particularly happy with this, but it works and can be easily reorganized later if an alternative is built into the structure.
In your HTML:
<%= Html.SubmitImage("Dog", "~/images/dog.jpg")%> <%= Html.SubmitImage("Cat", "~/images/cat.jpg")%>
In the controller action method for the form:
(This is an action method. It's up to you how to implement each button)
public ActionResult Index(FormCollection form) { // get the name of the button that was clicked (sent via either Dog.x or Cat.x since its an image button) var buttonName = form.GetSubmitButtonName(); if (buttonName == "Remove" || buttonName == "Skip") { Remove(form); } else if (buttonName == "Add") { Add(form); } }
Extension Method:
(Here we found a form parameter called Remove.x or Skip.x or Add.x and removes the .x part)
public static class FormCollectionExtensions { public static string GetSubmitButtonName(this FormCollection formCollection) { var button = formCollection.Keys.OfType<string>().Where(x => x.EndsWith(".x")).SingleOrDefault();
Note. One alternative is to use CSS to place the background image on a regular type of Html.SubmitButton button (a regular HTML button instead of an HTML image button), but I did not find it satisfactory for me because of the different ways different browsers behave ( see this question ).
Simon_Weaver
source share