Monday, May 11, 2009

Martin Fowler: Any fool can write code that a computer can understand. Good programmers write code that humans can understand


ActionButton with Controller name and Action name in ASP.NET/MVC

using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

public static class Action
public static string ActionButton(this HtmlHelper helper, string Url, string Text)
return GetHtmlBy(Url, Text);

public static string ActionButton(this HtmlHelper helper, string ControllerName, string ActionName, string ButtonText)
var urlhelper = new UrlHelper(helper.ViewContext.RequestContext);
string virtualpath = string.Format("~/{0}/{1}", ControllerName, ActionName);
string absurl = urlhelper.Content(virtualpath);
return GetHtmlBy(absurl, ButtonText);

private static string GetHtmlBy(string Url, string Text)
string outputUrl = string.Format(@"
<input type='button' onclick=""javacript: window.location='{1}';"" value='{0}' />", Text, Url);
return outputUrl;

You can use this ActionButton in aspx file:

<%=Html.ActionButton("ControllerName","ActionName","ButtonText") %>