Display Image In MVC Using Custom Image HTML Helper

In this blog, I am going to explain about Custom Image HTML Helper which can be created using extension method according to the project's need and it can be used like other normal HTML helpers.

To demonstrate Custom Image HTML Helper, I have created a static class and static method. Custom Image Helper method returns MvcHtmlString and it will take a few parameters which will be used for creating new tag builders like src, alt, height, width and htmlhelper class which is used with this keyword so we can add extension method on that class.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace CustomeHtmlHelper  
  8. {  
  9.     public static class CustomeImageHelper  
  10.     {  
  11.         public static MvcHtmlString Image(this HtmlHelper helper, string id, string src, string alt, string height, string width)  
  12.         {  
  13.             // Below line is used for generate new tag with img  
  14.             var builder = new TagBuilder("img");  
  15.   
  16.             // Below five lines are used for adding atrribute for img tag  
  17.             builder.MergeAttribute("id", id);  
  18.   
  19.             builder.MergeAttribute("src", src);  
  20.   
  21.             builder.MergeAttribute("alt", alt);  
  22.   
  23.             builder.MergeAttribute("height", height);  
  24.   
  25.             builder.MergeAttribute("width", width);  
  26.   
  27.             // this method will return MVChtmlstring and Create method will render as selfclosing tag.  
  28.   
  29.             return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));  
  30.         }  
  31.     }  
  32. }  
Note

MvcHtmlString class inherits HtmlString and HtmlString class implements IHtmlString interface.

Notice the below code snippet for TextBox HTML helper. If we want to display image in MVC we do not have any HTML helper for an image which we can use like TextBox HTML helper. In the below snippet if we try to use Image helper we do not have an option for image.
 
ASP.NET
 
If we want to display images in MVC, then we have to use img control as below in the View. 
  1. <img src="@Url.Content(ImagePath)" alt="@Model.AlternateText" />  
Now, here I will use custom image HTML helper which we have created as above. If we want to use a custom helper in all views then we need to add the below highlighter line in web.config file under <system.web> section.
 
ASP.NET 
 
After adding this line we will get our custom Image helper or you can import @using CustomeHtmlHelper.CustomeImageHelper on the top of your View. Now, you will be able to see our custom image HTML helper as below
 
ASP.NET 

Here is final image helper with all parameters which we can pass to image extension method, you can use this HTML helper like another helper.
 
ASP.NET 
 
I hope it helps you.
 
If you have any questions on this please feel free to ask me by posting comments down below.
 
Thank you for reading.