Here is my updated AcceptParameterAttribute class with support for data bound lists:
some sample code to use this updated version:
decorate the action with something like:
and then the buttons in the list can have the following format for the name:
hth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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); | |
} | |
} | |
} |
decorate the action with something like:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[AcceptParameter(Action = "DeleteSlot", ActionRegex = "DeleteSlot:[a-zA-Z0-9]{1,4}")] | |
public ActionResult Index_DeleteSlot(EventScheduleTabViewModel model){ | |
... | |
} |
and then the buttons in the list can have the following format for the name:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="submit" value="delete" name='DeleteSlot:@slot.Id' /> |
Comments
Post a Comment