0

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?

tbddeveloper
  • 2,407
  • 1
  • 23
  • 39

2 Answers2

1

Implement IHtmlString interface to your classes. However note that writing directly to output is faster, so keep that in mind for large controls.

Lukáš Novotný
  • 9,012
  • 3
  • 38
  • 46
  • I'm thinking that writing to the output is the way I want to go, but I can't quite see how I achieve that. How do I get the ability to write to output but put; @Html.Factory().Grid() in my view? – tbddeveloper Feb 08 '11 at 15:58
  • @ expects value to write.. you can override ToString (or ToHtmlString with IHtmlString), write stuff directly into output and return string.Empty. It will work, but I personally don't consider it clean solution. – Lukáš Novotný Feb 08 '11 at 16:07
  • I'm not worried about getting this done right now, I'm after the clean solution. I guess I'm missing something with how Telerik manage it. – tbddeveloper Feb 08 '11 at 16:10
  • I don't know if there is any newer version, but that opensource Telerik controls simply writes stuff to output and has no return value, which means you will have to call it as @{Html.Telerik().Grid().Render();} - which is in my opinion the correct way. Look at Html.Render* - same thing. If you insist on writing directly to output with just @Html.Factory().Grid() either override ToString/ToHtmlString, write to output and return string.Empty or make it like @Html.Factory().Grid().Render() and do the same - write stuff to output and return string.Empty. That is probably the cleanest way to do it. – Lukáš Novotný Feb 08 '11 at 16:35
1

override the implicit cast to string and return Render:

public static implicit operator string(MyTest.Extensions.Grid g)
{
    return g.Render();
}

or override the ToString():

public string ToString()
{
    return g.Render();
}
astellin
  • 433
  • 3
  • 13