Wednesday, October 6, 2010 10:02 PM Vimpyboy

Declarative helpers in Razor

ASP.NET Web Pages Beta 1 have support for Helpers, which are methods written in C# or Visual Basic, and returns IHtmlString. They can return HTML that is displayed on the page. If you choose to create helpers that way, you would need to have your HTML in an assembly, which makes it different to modify. Because of that, the new version of Razor supports declarative Helpers that you can create directly on the page. They work in both ASP.NET MVC 3 Beta and ASP.NET Web Pages Beta 2, which is included in WebMatrix.

A declarative helper is created the same way as a method, but instead of having a return type, you set ”helper”.

Let´s say you have this code:

@{
    var text = "Hello, world!";
    <p>@text</p>
}

We want to refactor it into its own method to be able to reuse it. We can do it using a declative helper, like this:

@helper SayHello(string yadda)
{
    <p>Text: @yadda</p>
}

And if you want to use it:

@SayHello("Hello, world!")

We can also add code in the helper, such as:

@helper Calculator(int a, int b)
{
    @{
        var sum = a + b;
    }
    
    <b>@sum</b>
}

And to display the result, we use:

1 + 2 = @Calculator(1, 2)

It will display ”3” on the page, which is the sum of the numbers.

To go one step further and make it even more dynamic, we can use Lambda Expressions for the helper. If we want to be able to change how to count the result, we could use this instead:

@helper Calc(int a, int b, Func<int, int, int> calc)
{
    @{
        var sum = calc(a, b);
    }
    <p>@sum</p>
}

And if we want to multiply the letters, we use the helper this way:

@Calc(4, 4, (a, b) => a * b)

We can actually create really advanced helpers directly on the page. This is for those who are using ASP.NET Web Pages, but it can also be used in ASP.NET MVC 3 Beta.

Filed under: , , , ,

Comments

# re: Declarative helpers in Razor

Wednesday, October 6, 2010 11:21 PM by camus

nice, but i'm waiting for simple "generic" functions in external files.

# re: Declarative helpers in Razor

Thursday, October 7, 2010 3:39 AM by Vimpyboy

Hi camus,

Sorry, but I dont really understand what you mean. Do you have an example of how you would to that?

# re: Declarative helpers in Razor

Tuesday, October 19, 2010 5:26 PM by Mikael

Camus mean shared helpers that can be used in different views like a utility class.

# re: Declarative helpers in Razor

Tuesday, November 2, 2010 5:07 PM by Sean Paul

Is it possible to call an existing Html helper such as TextBoxFor etc from within the declarative helper?

# re: Declarative helpers in Razor

Wednesday, November 3, 2010 9:28 AM by Vimpyboy

Hi Sean,

You should be able to do that, but using an EditorTemplate is much better for that scenario.

/Mikael Söderström

# re: Declarative helpers in Razor

Wednesday, November 10, 2010 8:18 AM by Johann

I'm confused about how to "share" declarative helpers in MVC 3 RC. What I thought we would be able to do is create a cshtml file somewhere with a declarative helper that can be accessed by all views. I tried putting them in /Views/Shared but that doesn't work.   Or maybe I'm missing something in the cshtml file itself (the one that contains the helper to be shared)?

Can you clarify?