Extending the ASP.NET MVC HtmlHelper

The HtmlHelperobject in MVC is great if we want to have a link to an action on a controller we simply do:


htmlhelper extension manual

And this will create us a link with the text ‘my link’ and will automatically route the href to the ViewItem action on the controller in context. However the HtmlHelper object doesn’t have everything and there are going to be times when you want to have your own HTML rendered on screen, so how can we go about doing that.

The answer lies with extension methods, we can use these to extend the HtmlHelper object and provide us with what were after, in my case it was that I wanted a link but an image enclosed instead of just text.

So here’s the code to achieve this:

public static class HtmlHelperExtensions
{
    public static string ImageActionLink(this HtmlHelper helper, string imagePath, string actionName, string controllerName, object routeValues, object imageAttributes, object linkAttributes)
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
        var anchortagBuilder = new TagBuilder("a");

        anchortagBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
        anchortagBuilder.MergeAttribute("href", urlHelper.Action(actionName, controllerName, routeValues));

                var imageTagBuilder = new TagBuilder("img");
        imageTagBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));
        imageTagBuilder.MergeAttribute("src", imagePath);

        anchortagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);
        return anchortagBuilder.ToString(TagRenderMode.Normal);
    }
}

The good thing is that we get a few objects to help us build up the html and to handle the routing side of things rather than having deal with concatenating strings.

Notice that I use the RouteValueDictionary object so that I can use the anonymous type to dictionary trick, the UrlHelper deals with returning the routed URL I just need to provide the controller and what action to invoke, and the TagBuilder makes  light work of creating HTML.

So now from my view I can use it like this:


I could provide some overloads so that I don’t need to pass empty objects and to accept Dictionaries instead of just object, but you get the idea.

One other thing that’s worth doing is changing the web.config do that your namespace is added automatically to your views that saves having to put the Import on each view that wants to use your extensions, this can be found in the pages section in namespaces.

Advertisements

7 thoughts on “Extending the ASP.NET MVC HtmlHelper

  1. Pingback: Extending the ASP.NET MVC HtmlHelper in VB.NET Specifically
  2. US dollars pedo kid rape Okay… He´s fat, she´s gorgeous blah, blah, blah… I don´t know if he paid her or not, but he´s one happy fat ass. And isn´t it hot when she´s doing cow girl? Like she climbed on a hill LOL. Anyway, I like it.

  3. A feature-rich option to harness creativity and concentration through the
    Alpha state is Alpha – Mind. You get excellent health and you won’t fall sick easily.
    My favorite source of information on medical research is
    the Pub – Med database at the U.

Comments are closed.