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?

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

How to animate tree icons in Umbraco v8

This post shows you how you can change the colour and animate tree icons in Umbraco based on a prope…

Read Post

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to fix the 403 error on preview in Umbraco after upgrading

This post shows you how to fix the 403 error on preview after you have upgraded it.

Read Post