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)
{
TagBuilder form = new TagBuilder("form");
form.Attributes.Add("method", "get");
form.Attributes.Add("action", url);
TagBuilder button = new TagBuilder("input");
button.Attributes.Add("type", "submit");
button.Attributes.Add("value", buttonText);
if (null != htmlAttributes)
{
foreach (KeyValuePair<string, Object> htmlAttribute in htmlAttributes)
{
button.Attributes.Add(htmlAttribute.Key, htmlAttribute.Value.ToString());
}
}
form.InnerHtml = button.ToString(TagRenderMode.SelfClosing);
return MvcHtmlString.Create(form.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.