ASP.NET MVC ActionButton

Sometimes a button is more representative of an action than a link is. With the following code, switching between a link and a button is a matter of changing Html.ActionLink into Html.ActionButton. The ActionButton class implements all ActionLink overloads.

The control emits a form with the method set to GET.

ActionButton Helper

using System.Web.Routing;
using System.Collections.Generic;

namespace System.Web.Mvc
{
	public static class LinkExtensions
	{

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName);
			return ActionButtonInternal(buttonText, a, null);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, Object routeValues)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, routeValues);
			return ActionButtonInternal(buttonText, a, null);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, string controllerName)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controllerName);
			return ActionButtonInternal(buttonText, a, null);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName,
				RouteValueDictionary routeValues)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, routeValues);
			return ActionButtonInternal(buttonText, a, null);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				Object routeValues, Object htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, routeValues);
			return ActionButtonInternal(buttonText, a, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				RouteValueDictionary routeValues, IDictionary<string, Object> htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, routeValues);
			return ActionButtonInternal(buttonText, a, htmlAttributes);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				string controllerName, Object routeValues, Object htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controllerName, routeValues);
			return ActionButtonInternal(buttonText, a, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				string controllerName, RouteValueDictionary routeValues, IDictionary<string, Object> htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controllerName, routeValues);
			return ActionButtonInternal(buttonText, a, htmlAttributes);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				string controllerName, string protocol, string hostName, string fragment, 
				Object routeValues, Object htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controllerName, new RouteValueDictionary(routeValues), protocol, hostName) + "#" + fragment;
			return ActionButtonInternal(buttonText, a, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				string controllerName, string protocol, string hostName, string fragment, 
				RouteValueDictionary routeValues, IDictionary<string, Object> htmlAttributes)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controllerName, routeValues, protocol, hostName) + "#" + fragment;
			return ActionButtonInternal(buttonText, a, htmlAttributes);
		}

		public static MvcHtmlString ActionButton(this HtmlHelper htmlHelper,
				string buttonText, string actionName, 
				string controller, object routeValues)
		{
			string a = (new UrlHelper(htmlHelper.ViewContext.RequestContext)).Action(actionName, controller, routeValues);
			return ActionButtonInternal(buttonText, a, null);
		}

		private static MvcHtmlString ActionButtonInternal(string buttonText,
				string url, IDictionary<string, Object> htmlAttributes)
		{
			string queryParamString = null;
			if (url.Contains("?"))
			{
				queryParamString = url.Substring(url.IndexOf("?") + 1);
				url = url.Substring(0, url.IndexOf("?"));
			}

            TagBuilder formBuilder = new TagBuilder("form");
            formBuilder.Attributes.Add("method", "get");
            formBuilder.Attributes.Add("action", url);

            TagBuilder buttonBuilder = new TagBuilder("input");
            buttonBuilder.MergeAttributes(htmlAttributes);
            buttonBuilder.MergeAttribute("type", "submit");
            buttonBuilder.MergeAttribute("value", buttonText);

			if (null != queryParamString)
			{
				string[] queryParameters = queryParamString.Split('&');

				foreach (string queryParameter in queryParameters)
				{
					string[] queryParamParts = queryParameter.Split('=');

					TagBuilder hiddenBuilder = new TagBuilder("input");
					hiddenBuilder.Attributes.Add("type", "hidden");
					hiddenBuilder.Attributes.Add("name", queryParamParts[0]);
					hiddenBuilder.Attributes.Add("value", queryParamParts[1]);

					formBuilder.InnerHtml += hiddenBuilder.ToString(TagRenderMode.SelfClosing);
				}

			}

			formBuilder.InnerHtml += buttonBuilder.ToString(TagRenderMode.SelfClosing);
			return MvcHtmlString.Create(formBuilder.ToString(TagRenderMode.Normal));
		}

	}

}
				

How to use


	@Html.ActionLink("Edit", "Edit", new { id = Model.Id })

	@Html.ActionButton("Edit", "Edit", new { id = Model.Id })

				

See the demo in my sandbox.