What's an Html Helper Method?

An Html Helper Method is a method which returns a string or IHtmlString, usually. If you are using MVC, you will have seen it a lot already with the likes  of @Html.TextBoxFor or @Html.LabelFor etc etc.

What you pass into the method is then used and returned back to the view. In this example I am going to show you how to create one for a Breadcrumb.

Here's an example for a breadcrumb

First of all we need to create a class somewhere.

using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Umbraco.Core.Models;
using Umbraco.Web;
namespace CodeShare.Library.Helpers.General {     public static class HtmlExtensions     {         public static IHtmlString Breadcrumb(this HtmlHelper helper, IPublishedContent thisPage)         {             StringBuilder breadcrumb = new StringBuilder();
            IEnumerable<IPublishedContent> ancestors = thisPage.Ancestors().Reverse();
            foreach(IPublishedContent page in ancestors)             {                 breadcrumb.Append($"<a href=\"{page.Url}\">{page.Name}</a>");             }
            return MvcHtmlString.Create(breadcrumb.ToString());         }     } }

Explaining the code above.

It needs to be a static class so it can be called at any time. The method needs to be static too.

The first parameter HtmlHelper helper uses the keyword this to mean be an extension of HtmlHelper.

The second parameter IPublishedContent thisPage is the content model to be passed in. If you are not using Umbraco, you might pass in a different parameter here for your page model.

It then loops through the ancestors of the page in reverse order and adds some html for an a tag for each item. And finally for thisPage it just adds it in a span.

You can use this on any view or partial view now and just pass in the object for thisPage, so if it is in a view it will be Model.Content


The markup it will return will be

<a href="/">Home</a><a href="/news/">News</a><span>Article</span>

Notice the return line where it says

return MvcHtmlString.Create(breadcrumb.ToString());

This makes it return IHtmlString instead of just returning the string.

So it renders in the view as Html rather than rendering it as a string.


Now you can see how easy they are to create, you can use them yourself to create really useful helpers.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

Find out how my blog reached 1 million page views

In this post I will tell you how my blog reached 1 million page views.

Read Post

How to include scripts from partial views in MVC and Umbraco

This post will show you how you can include scripts from inside an MVC partial view rather than havi…

Read Post

How to fix the error "Authentication failed because the remote party has closed the transport stream"

This post will show you how to fix the error "Authentication failed because the remote party has clo…

Read Post

How to solve the error Could not load file or assembly System.Security.Principal.Windows

This post help you solve the error System.IO.FileNotFoundException Could not load file or assembly S…

Read Post