Is there some easy way to handle multiple submit buttons from the same form? For example:
<% Html.BeginForm(\"MyAction\", \"MyController\", FormMethod.Pos
this is the best way that i have found:
http://iwayneo.blogspot.co.uk/2013/10/aspnet-mvc-action-selector-with-list.html
Here is the code:
///
/// ActionMethodSelector to enable submit buttons to execute specific action methods.
///
public class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
///
/// Gets or sets the value to use to inject the index into
///
public string TargetArgument { get; set; }
///
/// Gets or sets the value to use in submit button to identify which method to select. This must be unique in each controller.
///
public string Action { get; set; }
///
/// Gets or sets the regular expression to match the action.
///
public string ActionRegex { get; set; }
///
/// Determines whether the action method selection is valid for the specified controller context.
///
/// The controller context.
/// Information about the action method.
/// true if the action method selection is valid for the specified controller context; otherwise, false.
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
Func formGetter;
Func queryStringGetter;
ValidationUtility.GetUnvalidatedCollections(HttpContext.Current, out formGetter, out queryStringGetter);
var form = formGetter();
var queryString = queryStringGetter();
var req = form.AllKeys.Any() ? form : queryString;
if (!string.IsNullOrEmpty(this.ActionRegex))
{
foreach (var key in req.AllKeys.Where(k => k.StartsWith(Action, true, System.Threading.Thread.CurrentThread.CurrentCulture)))
{
if (key.Contains(":"))
{
if (key.Split(':').Count() == this.ActionRegex.Split(':').Count())
{
bool match = false;
for (int i = 0; i < key.Split(':').Count(); i++)
{
if (Regex.IsMatch(key.Split(':')[0], this.ActionRegex.Split(':')[0]))
{
match = true;
}
else
{
match = false;
break;
}
}
if (match)
{
return !string.IsNullOrEmpty(req[key]);
}
}
}
else
{
if (Regex.IsMatch(key, this.Action + this.ActionRegex))
{
return !string.IsNullOrEmpty(req[key]);
}
}
}
return false;
}
else
{
return req.AllKeys.Contains(this.Action);
}
}
}
Enjoy a code-smell-less multi submit button future.
thank you