Wednesday, October 06, 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

# Twitter Trackbacks for Declarative helpers in Razor - Mikael S??derstr??m [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 Declarative helpers in Razor - Mikael S??derstr??m         [asp.net]        on Topsy.com

# re: Declarative helpers in Razor

Wednesday, October 06, 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 07, 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 02, 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 03, 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?  

# re: Declarative helpers in Razor

Thursday, November 11, 2010 2:07 PM by Andy Heimdahl

Johann

I was able to share declarative helpers by adding an "App_Code" folder to my project and putting my cshtml helper files in there.  I did have to add a couple dummy classes to my project to work around a compilation issue as described here:  stackoverflow.com/.../razor-helper-in-mvc-3-rc

Hope that helps.

-andy

# Good Articles

Monday, March 07, 2011 12:33 PM by zc0000

How jQuery selects elements using Sizzle Customizing ASP.NET MVC 2 - Metadata and Validation ASP.NET

# T4 vs Razor – what’s the skinny?

Thursday, March 10, 2011 9:43 PM by [Profoundly Esoteric Image]

You might have noticed in the recent storm of Web Tools releases that there’s a groovy new kid on the

# T4 vs Razor – what’s the skinny?

Friday, March 25, 2011 1:08 AM by Official T4 team blog

You might have noticed in the recent storm of Web Tools releases that there’s a groovy new kid on the

# Bad Razor, BAD! | D-Flat

Sunday, May 29, 2011 11:13 PM by Bad Razor, BAD! | D-Flat

Pingback from  Bad Razor, BAD! | D-Flat

# re: Declarative helpers in Razor

Monday, May 14, 2012 12:39 AM by sökmarknadsföring

Hej, det är verkligen intressant, tack

# re: Declarative helpers in Razor

Monday, May 14, 2012 2:14 AM by webbtrafik

Denna artikel är bokmärke värdig enligt min åsikt. Det är värt att spara för framtida referens. Det är fascinerande läsning med många giltiga poäng för kontemplation. Jag måste instämma på nästan varje punkt görs inom denna artikel.

# re: Declarative helpers in Razor

Thursday, May 17, 2012 8:28 AM by Mihai Moisei

This helpers are great

Leave a Comment

(required) 
(required) 
(optional)
(required)