Click here to Skip to main content
12,761,780 members (39,730 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 3 Feb 2014

Custom HTML Helper for MVC Application

, 3 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Add an HTML helper and make it available to your entire application


How many times did you wonder how to avoid writing the same chunk of HTML / JavaScript code many times in an MVC Razor view? What if this repetitive task includes some logic?

The solution to avoid this is to build a custom HTML helper to encapsulate everything and make the repetitive task less cumbersome.

Let's say we want to avoid doing this many times inside your views:

<img src="@myModel.MyObject.ImageSource", alt ="@myModel.MyObject.Imagename",
title ="@myModel.MyObject.ImageDescription" />  

And instead, you want something Razor like that may take care of some logic or validations to avoid error because in the previous snippet, the ImageSource or Imagename or again the description may be null:

@myModel.MyObject.Imagename, @myModel.MyObject.ImageDescription) 
namespace MyNamespace 
    public static class MyHeleprs
        public static MvcHtmlString Image(this HtmlHelper htmlHelper, 
        	string source, string alternativeText)
            //declare the html helper 
            var builder = new TagBuilder("image"); 
            //hook the properties and add any required logic
            builder.MergeAttribute("src", source);
            builder.MergeAttribute("alt", alternativeText);
            //create the helper with a self closing capability
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));

To make this helper available in your view, add its namespace as follows:

@namespace MyNamespace 

But if you want it available in all your views, you have to add the namespace not to a specific view but to the collection of namespaces in views' web.config:

<add namespace="MyNamespace" />   

Now, the Image helper is available everywhere in your views.


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


About the Author

Mack Ait-Aoudia
Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

Questionnamespace ... Pin
nev3087-Oct-14 12:17
membernev3087-Oct-14 12:17 
QuestionGood post Pin
Ayan Chalki25-Jun-14 4:13
memberAyan Chalki25-Jun-14 4:13 
Questionit's all about logic Pin
Mack Ait-Aoudia6-Feb-14 11:54
memberMack Ait-Aoudia6-Feb-14 11:54 
QuestionNice Intro to extending HtmlHelper, but I think not necessary for the problem shown Pin
igambin5-Feb-14 5:51
memberigambin5-Feb-14 5:51 
QuestionVery good article Mack Ait! Pin
Volynsky Alex4-Feb-14 10:28
professionalVolynsky Alex4-Feb-14 10:28 
QuestionMmmm Pin
Sacha Barber4-Feb-14 3:22
mvpSacha Barber4-Feb-14 3:22 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 3 Feb 2014
Article Copyright 2014 by Mack Ait-Aoudia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid