I'm trying to compile my own set of controls in MVC3. So, rather than create HTML Helper extensions for all of them, I figured I would mimic the behavior of other frameworks (Telerik, DevExpress). So I created a new helper extension to offer up my Item class;
public static IFriendlyComponentFactory Factory(this HtmlHelper helper)
{
return new FriendlyComponentFactory(helper.ViewContext);
}
And then, from within here I offer up my object so I can set properties etc.
If I put @Html.Factory().Grid( ).Name( "Test")
Then I get;
MyTest.Extensions.Grid
Which I understand is the ToString() behavior. So, I change the ToString to output;
new MvcHtmlString("<h3>Test</h3>").ToString()
Ok, I get the HTML fully encoded, so I see my tags and all in the page. Hmm.
I can see in the Telerik source(I'm trying to figure this out for myself) that they are dealing with ToString( ) and appear to be outputting HTML, but I can see work gets done in the render to output to the ViewContext Writer, rather than the output of ToString().
Finally, I tried;
@{ Html.Factory().Grid().Render( ); }
And I wrote directly to the ViewContext.Writer. Tada, I got HTML output.
But, I don't want to have to wrap code braces around, I'd like to understand how to provide the cleaner interface.
Any ideas how I can provide this?