Skip to content

Instantly share code, notes, and snippets.

@Dkowald
Created November 9, 2011 22:21
Show Gist options
  • Select an option

  • Save Dkowald/1353315 to your computer and use it in GitHub Desktop.

Select an option

Save Dkowald/1353315 to your computer and use it in GitHub Desktop.
SubmitActionHandler - MVC multiple submit buttons.
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:
/// &lt;input type='submit' name='action' value='MyAction' /&gt;
/// </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