문제

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

thx

도움이 되었습니까?

해결책

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")

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top