In standard MVC, a view (page) can only have one action. You retrieve (GET) the view and submit it (POST). Therefore only one POST-action can be triggered, and if you want multiple buttons in a single view you need to dive into the form values to differentiate between them and use a switch or something inside the POST-action.
It turns out there is a way to have multiple submit buttons that each trigger a distinct action though. You can put an attribute on the two POST-actions that lets MVC pick the right one depending on which button you clicked. It is important to know though that you can't have the standard submit action you would normally have:
[HttpPost] public ActionResult Index() { }Remove this action if you have it. It will cause a conflict between this standard action and the one for the submit button.
The attribute
First you need to create the following attribute class. I called it Submit:public class SubmitAttribute : ActionNameSelectorAttribute { public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { if (actionName == methodInfo.Name) return true; else return controllerContext.RequestContext.HttpContext.Request[methodInfo.Name] != null; } }
The actions
Second, create the actions for - in this example - two submit buttons:[Submit] [HttpPost] public ActionResult SaveProjectFilter(ProjectModel model) { } [Submit] [HttpPost] public ActionResult DeleteProjectFilter(ProjectModel model) { }Notice I decorated both actions with my Submit attribute.
The buttons
Third, put these two submit buttons in the view:<input type="submit" name="SaveProjectFilter" value="@Dictionary.Get("Save")" class="icon-save" /> <input type="submit" name="DeleteProjectFilter" value="@Dictionary.Get("Delete")" class="icon-delete" />Notice how the buttons' names match their respective actions. This is the key to the whole thing! This is actually it. If you click any of the two buttons the proper action will execute.