Image ActionLink HTML Helper in ASP.NET MVC

The best part of the asp.net mvc framework is that it is extensible. You can extend the things according to your business requirements. 

For creating a link in ASP.NET MVC, we use Link Extension provided by the MVC framework. For creating a simple anchor tag, we use Html.ActionLink() helper which generates an anchor tag for us.

For example:

  1. @Html.ActionLink("Link Display Text","Index","Home")  

This will render the following html:

  1. <a href="/Home/Index">Link Display Text</a>  

Now if we want to create an image link, we can use html to create image link this way:

  1. <a href="/Home/Index">  
  2. <img src="/images/untitled.png">  
  3. </a>  

This will work obviously, but what if we want to do it with Html Helper method as we do using Html.ActionLink, but we do not have any helper for the image link provided by framework? One can do it using html as I wrote above, or  you will have to write a custom html helper.

It's the beauty of ASP.NET MVC that we can extend existing helpers according to our needs and can also add new html helpers in case we need to use it in many places. In that case, a better approach is to create a helper extension and use it everywhere in the project.

Now for creating a helper extension, first of all, we need to create a static class, as it's the pre-requisite for creating extension methods that the method should be static, and it should be inside a static class.

Here is the code for extension method for ImageActionLink helper extension:

  1. namespace MyApplication.Helpers  
  2.   
  3. {  
  4.   
  5.     public static class CustomHtmlHelepers  
  6.   
  7.     {  
  8.   
  9.         public static IHtmlStringImageActionLink(this HtmlHelperhtmlHelper,  
  10.   
  11.             string linkText, string action, string controller,  
  12.   
  13.             object routeValues, object htmlAttributes, stringimageSrc)  
  14.   
  15.         {  
  16.   
  17.             varurlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);  
  18.   
  19.             varimg = new TagBuilder("img");  
  20.   
  21.             img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));  
  22.   
  23.             var anchor = new TagBuilder("a")  
  24.   
  25.             {  
  26.                 InnerHtml = img.ToString(TagRenderMode.SelfClosing)  
  27.             };  
  28.   
  29.             anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);  
  30.   
  31.             anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));  
  32.   
  33.             returnMvcHtmlString.Create(anchor.ToString());  
  34.   
  35.         }  
  36.   
  37.     }  
  38.   
  39. }  

The first parameter with this keyword is used in extension methods. See this article on Extension Method for understanding what extension methods are.

And now, we can call it from View for creating image link. Remember that we will have to include the namespace in the View in which we have to create the class and extension method, but if you have to create it in the same namespace, then it will be accessible without adding any using statement in View.

In my case, I have separated all Extension Methods in a separate namespace named Helpers, so I will have to include it in View via using statement:

  1. @using MyApplication.Helpers;  
  2.   
  3. @Html.ImageActionLink("Link Display Text","Index","Home",null,null,"~/images/untitled.png")  

It will render the same html which I wrote above::

  1. <a href="/Home/Index">  
  2. <img src="/images/untitled.png">  
  3. </a>  

I hope this helps you understand how to write your own custom HTML Helpers in ASP.NET MVC.


Similar Articles