Created
November 9, 2011 22:21
-
-
Save Dkowald/1353315 to your computer and use it in GitHub Desktop.
SubmitActionHandler - MVC multiple submit buttons.
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
| namespace derek.kowald.MVC | |
| { | |
| using System; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using System.Web.Routing; | |
| /// <summary> | |
| /// Custom route handler so a form submit button can | |
| /// define the mvc action to take. | |
| /// </summary> | |
| /// <remarks> | |
| /// Be careful to only define submit buttons with the name action. | |
| /// </remarks> | |
| /// <example> | |
| /// Update your routes to use this hander: | |
| /// routes.MapRoute("Default", | |
| /// "{controller}/{action}/{id}", | |
| /// new { controller = "Home", action="Index", id = UrlParameter.Optional}) | |
| /// .RouteHandler = new SubmitActionHandler(); | |
| /// | |
| /// Update your markup so sumbit button(s) have the name action: | |
| /// <input type='submit' name='action' value='MyAction' /> | |
| /// </example> | |
| public class SubmitActionHandler : IRouteHandler | |
| { | |
| #region IRouteHandler Members | |
| /// <summary> | |
| /// If an input parameter named 'action' is provided, | |
| /// update the RouteData action property with the form data. | |
| /// </summary> | |
| public IHttpHandler GetHttpHandler(RequestContext requestContext) | |
| { | |
| string reqAction = null; | |
| if (requestContext.HttpContext != null && | |
| requestContext.HttpContext.Request != null) | |
| { | |
| reqAction = requestContext.HttpContext.Request["action"]; | |
| } | |
| if (!String.IsNullOrEmpty(reqAction)) | |
| { | |
| requestContext.RouteData.Values["action"] = reqAction; | |
| } | |
| return new MvcHandler(requestContext); | |
| } | |
| #endregion | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment