Click here to Skip to main content
15,891,719 members
Articles / Web Development / HTML

Image ActionLink HTML Helper in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
12 Nov 2015CPOL2 min read 32.2K   14   3
Image ActionLink HTML helper in ASP.NET MVC

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 anchor tag for us.

For example:

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

This will render the following html:

<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:

HTML
<a href="/Home/Index">
<img src="/images/untitled.png">
</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 image link provided by framework, so one can do it using html as I wrote above or will have to write 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 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:

namespace MyApplication.Helpers
{
  public static class CustomHtmlHelepers
  {
    public static IHtmlString ImageActionLink(this HtmlHelper htmlHelper, 
    string linkText, string action, string controller, 
    object routeValues, object htmlAttributes,string imageSrc)
    {
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var img = new TagBuilder("img");
        img.Attributes.Add("src", VirtualPathUtility.ToAbsolute(imageSrc));
        var anchor = new TagBuilder("a") 
        { InnerHtml = img.ToString(TagRenderMode.SelfClosing) };
        anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
        anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));

        return MvcHtmlString.Create(anchor.ToString());
    }
  }
}

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

And now, we can call it from View for creating Image Link. But 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 separate namespace named Helpers so I will have to include it in View via using statement:

CSS
@using MyApplication.Helpers;

@Html.ImageActionLink("Link Display Text",
"Index","Home",null,null,"~/images/untitled.png")

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

HTML
<a href="/Home/Index">
<img src="/images/untitled.png">
</a>

I hope it makes you understand how you can write you own custom html helpers in ASP.NET MVC.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Ehsan Sajjad is a Microsoft Certified Professional, Microsoft Certified C# Specialist and he is also among the top users on StackOverflow from Pakistan with 50k+ reputation at time of writing this and counting.

He is a passionate software developer with around 5 years of professional experience in Microsoft Technologies both web and desktop applications and always open to learn new things and platforms especially in mobile application development and game development.

Some Achievements :

  • 5th Top Contributor from Pakistan on Stackoverflow.com
  • Top User in ASP.NET MVC from Pakistan on Stackoverflow.com
  • 21st June 2017 - Article of the Day - ASP.NET Community (Beginners Guide on AJAX CRUD Operations in Grid using JQuery DataTables in ASP.NET MVC 5)
  • 19th April 2017 - Article of the Day - ASP.NET Community (ASP.NET MVC Async File Uploading using JQuery)
  • March 2017 - Visual C# Technical Guru Silver Medal on Microsoft Tech Net Wiki Article Competition
  • 20 January 2017 - Article of the Day - ASP.NET Community (Async File Uploading in ASP.NET MVC)
  • 22nd September 2016 - Article of the Day - ASP.NET Community (GridView with Server Side Filtering, Sorting and Paging in ASP.NET MVC 5)
  • 22nd August 2016 - Article of the Day - ASP.NET Community (Beginners Guide for Creating GridView in ASP.NET MVC 5)
  • December 2015 - C-SharpCorner Monthly Winner

Comments and Discussions

 
Questionhow can add css to image Pin
Hanafy8415-Apr-19 1:05
Hanafy8415-Apr-19 1:05 
QuestionVery nice Pin
AnHund11-Apr-16 21:25
AnHund11-Apr-16 21:25 
AnswerRe: Very nice Pin
Ehsan Sajjad11-Apr-16 23:57
professionalEhsan Sajjad11-Apr-16 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.