Last Updated: October 16, 2018
·
1.121K
· diegodfsd

Multiple submit buttons

Yesterday, I needed do a form with two submit buttons. Each button have to post to one different action. To solve this problem I implemented an actionSelector thus:

public class HttpParameterSelector : ActionNameSelectorAttribute 
{
    public string Name { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var parameterValue = controllerContext.HttpContext.Request[Name];

        return !string.IsNullOrWhiteSpace(parameterValue) && 
            parameterValue.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase);
    }
}

It works based in the parameters sent in querystring or form.

This is my razor form:

@using (Html.BeginForm())
{
    <input type="submit" name="action" value="Disapprove" />
    <input type="submit" name="action" value="Approve" />
}

In my controller I'm using my action selector like thus:

[HttpParameterSelector(Name = "action")]
public ActionResult Approve()
{ }
[HttpParameterSelector(Name = "action")]
public ActionResult Disapprove()
{ }

In this case, my action selector chooses based in "action" parameter.

1 Response
Add your response

In this case, my view is not globalized, but I can use a hidden field, querystring or form parameters, use only name of your submit button and more.

over 1 year ago ·