开发者

Rendering templated HtmlHelper with MVC3/Razor

I have a HtmlHelper that renders a grid as a table, in MVC2 I would call it from the view using the following syntax...

<%=
    Html.MyLibrary().Grid<MyGridItem>()
        .Name("MyGrid")
        .Width("100%")
        .Render()
%>

Notice it uses templates. In MVC3 with Razor it won't compile, seemingly because the left angle bracket '<' is being confused for html. To get around this I can enclose it in curly braces...

@{Html.MyLibrary().Grid<MyGridItem>()
        .Name("MyGrid")
        .Width("100%")
        .Render();}

but now the problem is that the string returned from .Render() doesn't get put in the output stream!

An开发者_开发技巧y help solving this would be much appreciated.


You should use Html.Raw() Method.

@Html.Raw(Html.MyLibrary().Grid<MyGridItem>()
        .Name("MyGrid")
        .Width("100%")
        .Render())

The @{...} server side block does not get rendered because it's simple server side code. Same if you wrote

@{ "renderedstring".ToString(); }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜