Question

Will existing HTMLHelpers work with Razor? If so, does Razor change anything about either the design or usage of HTMLHelpers?

thx

Was it helpful?

Solution

Yes, existing helpers work perfectly fine with Razor. For example:

@Html.ActionLink("foo bar", "foo")

What Razor changes is that now you have the possibility to define inline helpers like this:

@helper FooBar(string foo)
{
    <div>Hello @foo</div>
}

And use like this:

@FooBar("World")

As far as classic HTML helpers are concerned, Razor changes nothing, it's just a view engine so you continue to write your helpers as you've always did:

public static class HtmlExtensions
{
    public static MvcHtmlString FooBar(this HtmlHelper htmlHelper, string value)
    {
        var builder = new TagBuilder("div");
        div.SetInnerText(value);
        return MvcHtmlString.Create(div.ToString());
    }
}

And use in Razor:

@Html.FooBar("some value")

OTHER TIPS

Razor performs HTML Encoding by default.

So if any of your MVC2 HtmlHelpers emit markup, they might not work if they are returning String instead of MvcHtmlString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top