开发者

Rendering templated HtmlHelper with MVC3/Razor

开发者 https://www.devze.com 2023-02-25 11:33 出处:网络
I have a HtmlHelper that renders a grid as a table, in MVC2 I would call it from the view using the following syntax...

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

精彩评论

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