[CLOSED] Custom Ext.NET components as MVC Helpers?

  1. #1

    [CLOSED] Custom Ext.NET components as MVC Helpers?

    Hi,

    In some of my recent thread discussions we've been talking about custom components and they work great for Web Forms.

    I was looking at your source code to see how you create the MVC counterparts, and I think you have a series of builders and builder factory classes etc that help to form the helpers and they all get added to the X class. I presume they are auto-gen'd somehow? It looks neat (still need to study it more deeply).

    But, from what I can tell the BuilderFactory, Builder and various other classes you use are partial classes. If I understand that right I think that means I cannot use the same approach in my own project because partial classes have to be in the same project as they all get compiled into one class at compile time.

    If that is the case, what is the best way to make a custom Ext.NET component available as an MVC helper - or do you have any examples I can look at?

    Many thanks!
    Last edited by Daniil; Aug 08, 2012 at 9:49 AM. Reason: [CLOSED]
  2. #2
    Just to clarify, I can do something like this already:

    @Html.X().Viewport().Items(items => items.Add(new CustomGrid { Title = "My title" }) )
    ... which is great and covers many use cases. But what would also be nice is to be able to do things like @Html.CustomGrid().Title("My title") etc. Other than creating an MVC Helper by hand I wasn't sure if there was another technique or code from your library I can reuse?
  3. #3
    Please use extension methods
  4. #4
    Sorry, I am probably being a bit dud with my lack of ASP.NET MVC experience.

    I tried this:

    public static class FinancialGridExtensions
    {
        public static string FinancialGrid(this HtmlHelper helper, int height, int width)
        {
            return new FinancialGrid { Height = height, Width = width }.ToScript();
        }
    }
    (Where "FinancialGrid" is a subclass of GridPanelBase)

    Then I try to use it using something like this:

    @Html.FinancialGrid(400, 500);
    With .ToScript() I get an exception in DefaultScriptBuilder (line 330)

    I tried various alternatives to ToString (SelfRender, etc) but no joy.

    Any pointers to get me in the right direction?

    Thanks.
  5. #5
    Use SelfRender method
    return new FinancialGrid { Height = height, Width = width }.SelfRender();
    But if you want correct work in all cases then you have to return object implements IHtmlString interface and call SelfRender in the ToHtmlString
    Like this
    public class ControlRender : IHtmlString 
    {
          public ControlRender(BaseControl control)
          {
               this.Control = control;
          }
    
          public BaseControl Control
          {
                get;
                private set;
          }
    
          public string ToHtmlString()
          {
              return this.Control.SelfRender();
          }
    }
    
    public static IHtmlString FinancialGrid(this HtmlHelper helper, int height, int width)
    {
        return new ControlRender(new FinancialGrid { Height = height, Width = width });
    }
  6. #6
    I had tried SelfRender on its own many times and each time I did, template script was being output as HTML to the screen, something like this:

    <#:item ref="init_script" index="13">Ext.create("MyApp.FinancialGrid",{store:<#:anchor id="id1c1ef5e919c15df9_ClientInit" />,etc etc
    I then try your suggestion with IHtmlString/ControlRender etc - and it works. Learned something new here. Now I look at the documentation for IHtmlString I see it will not re-encode html strings, which makes sense for Ext.NET (and I also presume the &lt;# Item stuff gets pre-processed by your code somewhere, too, which is why it then works and puts this into the init script. I clearly need to find more time to study your code still!)

    Really appreciate the time, especially given how late you must have responded... do you ever sleep? :)

    You can mark this as closed.

Similar Threads

  1. How To Use MVC 3 And EXT.NET Components
    By stalsoft in forum 1.x Help
    Replies: 1
    Last Post: Mar 21, 2012, 2:48 AM
  2. Replies: 2
    Last Post: Jan 09, 2012, 7:18 AM
  3. [CLOSED] Custom event in custom tree panel not being generated
    By anup in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Dec 31, 2011, 8:45 PM
  4. [CLOSED] ASP.NET MVC - HTML Helpers
    By SymSure in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 26, 2011, 1:21 PM
  5. Add Components
    By flaviodamaia in forum 1.x Help
    Replies: 0
    Last Post: Sep 05, 2008, 11:37 AM

Tags for this Thread

Posting Permissions