Asp.Net Mvc action selector – with list support [REPOST]

Here is my updated AcceptParameterAttribute class with support for data bound lists:

/// <summary>
/// ActionMethodSelector to enable submit buttons to execute specific action methods.
/// </summary>
public class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
/// <summary>
/// Gets or sets the value to use in submit button to identify which method to select. This must be unique in each controller.
/// </summary>
public string Action { get; set; }
/// <summary>
/// Gets or sets the regular expression to match the action.
/// </summary>
public string ActionRegex { get; set; }
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>true if the action method selection is valid for the specified controller context; otherwise, false.</returns>
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
var req = controllerContext.RequestContext.HttpContext.Request.Form.AllKeys.Any() ? controllerContext.RequestContext.HttpContext.Request.Form : controllerContext.RequestContext.HttpContext.Request.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);
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub
some sample code to use this updated version:

decorate the action with something like:

[AcceptParameter(Action = "DeleteSlot", ActionRegex = "DeleteSlot:[a-zA-Z0-9]{1,4}")]
public ActionResult Index_DeleteSlot(EventScheduleTabViewModel model){
...
}
view raw gistfile1.cs hosted with ❤ by GitHub

and then the buttons in the list can have the following format for the name:

<input type="submit" value="delete" name='DeleteSlot:@slot.Id' />
view raw gistfile1.html hosted with ❤ by GitHub
hth

Comments