Home > Programming > Razor declarative helpers

Razor declarative helpers

I’m currently converting an old ASP.NET MVC 2 project to MVC 3. In my MVC 2 project, I created HTML helpers to generate small bits of HTML.

public static HtmlString SampleHtmlHelper(this HtmlHelper helper, string input)
    string result = "<div>";
    result += "<p>Current time: " + DateTime.Today.ToShortDateString() + "</p>";
    result += "<p>Input: " + input + "</p>";
    result += "</div>";
    return new HtmlString(result);

These helper classes would reside in a Helpers folder that I create.

To use the helper, I’d call it in a view like this:

@Html.SampleHtmlHelper("hello world")]

ASP.NET MVC 3 with the Razor view engine introduces a new way of creating declarative helpers that allows me to take advantage of the Razor syntax instead of appending strings together.

@helper SampleHelper(string input)
        <p>Current date: @DateTime.Today.ToShortDateString()</p>
        <p>Input: @input</p>

This helper would be placed in a view page in the App_Code folder.

I can treat each helper as a static method based on the view page name:

@Demo.SampleHelper("hello world")

I can convert most of my older HTML helpers to declarative helpers, but I’ve come across cases where I needed to continue creating helpers in code. I find that helpers that contain a lot of logic do not fit well with the Razor syntax.

public static HtmlString SampleHtmlHelper<TModel, TReturn>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TReturn>> expression)
    where TModel : class
    MemberExpression memberExpression = GetMemberExpression(expression);
    string name = memberExpression == null ? null : memberExpression.Member.Name;
    Func<TModel, TReturn> function = expression.Compile();
    TReturn value = function(helper.ViewData.Model);
    string result = "<div>";
    result += "<p>" + name + ": " + value + "</p>";
    result += "</div>";
    return new HtmlString(result);

Being able to leverage the Razor syntax for helpers is a real bonus. I don’t see a need to use partial views anymore. When I needed to decide whether to use a helper or a partial view, it would be determined by how much markup I needed to generate. I would typically use a HTML helper if the amount of markup wasn’t a burden or if it contained logic. But with the new declarative helpers, this isn’t an issue anymore. Are there any benefits that a partial view has over declarative helpers?

  1. tinel
    May 3, 2013 at 8:53 am

    thanks a lot good tutorial

  2. August 25, 2014 at 2:08 am

    Hello my loved one! I wish to say that this article is awesome, nice written and include almost
    all significant infos. I would like to see extra posts like
    this .

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: