My favourite helpers for ASP.NET MVC

As you use ASP.NET MVC on and on, you start building and using handy html helpers. Rob Conery posted a few of his helpers on his blog. You can find some more in his ASP.NET MVC source code. Inspired by his helpers, here I post mine.

1. Html.Image

Depending on the environment, the absolute url of an image can change. For example, on the dev machine, it is http://localhost:3107/content/images/cancel.gif. On Integration server, if the server host multiple applications under root with the same port numer, 80, the image path can be like http://intserver/app1/content/images/cancel.gif. In this case, the image would not be rendered. SiteRoot is Rob Conery’s helper.

To avoid the situation like the above, I reference image suing SiteRoot functionality.

public static string Image(this HtmlHelper helper, string imageName, string altText)
    var builder = new TagBuilder("img");
        UrlHelpers.SiteRoot(helper.ViewContext.HttpContext) + "/content/images/" + imageName);
    builder.MergeAttribute("alt", altText);

    return builder.ToString(TagRenderMode.SelfClosing);

2. Html.ClientIdFor

In MVC, you pass viewmodel to View. I often refactor the viewmodel and the change involve property name. When you change a property name of a viewmodel, all server side code like Html.TextBoxFor(m => m.Input.Firstname” change accordingly, but you have to manually change javascript code.

For example,


You would have to change the id manually. I thought it would be nice to specify clientId with server-side code, and I was not alone. There were guys with the same idea. The following is the code for ClientIdFor and I got the code from John Landheer‘s answer on stackoverflow.

public static MvcHtmlString ClientIdFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression)

3. Script

This writes javascript include statement.

public static string Script(this HtmlHelper helper, string fileName)
    if (!fileName.EndsWith(".js"))
        fileName += ".js";

    return string.Format("<script src='{0}/{1}/{2}/{3}' type='text/javascript' ></script>\n",
        helper.SiteRoot(), PUBDIR, SCRIPTDIR, helper.AttributeEncode(fileName));

public static string ScriptIe(this HtmlHelper helper, string fileName)
    if (!fileName.EndsWith(".js"))
        fileName += ".js";

    return string.Format("<!--[if lt IE 7]>\n<script src='{0}/{1}/{2}/{3}' defer type='text/javascript' ></script>\n<![endif]-->",
        helper.SiteRoot(), PUBDIR, SCRIPTDIR, helper.AttributeEncode(fileName));

— To be continued —


2 thoughts on “My favourite helpers for ASP.NET MVC

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s