An ASP.NET MVC ActionButton HtmlHelper Extension Method

Here's a quick ActionButton HtmlHelper extension method I wrote. Hopefully this will come in handy to someone.

For more information on extension methods, check out Microsoft's MSDN document here, or check out one of my earlier posts here.
public static MvcHtmlString ActionButton(
    this HtmlHelper htmlHelper, string value, object htmlAttributes, string actionName, 
    string controllerName, object routeValues) {     if (String.IsNullOrWhiteSpace(actionName))         throw new ArgumentException("actionName is required.");     if (String.IsNullOrWhiteSpace(controllerName))         throw new ArgumentException("controllerName is required.");     var button = new TagBuilder("input");     button.MergeAttribute("type""button");     button.MergeAttribute("value", value);     button.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));     if (!String.IsNullOrWhiteSpace(actionName))     {         var urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;         button.MergeAttribute("onclick""location.href='" + urlHelper.Action(
            actionName, controllerName, routeValues) + "'");     }     return new MvcHtmlString(button.ToString(TagRenderMode.SelfClosing)); } 
 
 
 

Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

Silent Renew and the "login_required" Error When Using oidc-client