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.
Written by Diego Dias
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mvc
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#