Search code examples
asp.net-mvc-3webgrid

MVC3 recursive nested webgrid


I'm having some trouble nesting webgrids.

As long as I only have to have 1 webgrid inside another, it's fine, but once I try to nest a webgrid in an already nested webgrid, it fails.

This is the code I have:

@grid.GetHtml(columns:
grid.Columns(
    grid.Column("Name"),
    grid.Column("Variants", format: (item) =>
    {
        WebGrid varGrid = varGrid = new WebGrid(item.Variant);
        return varGrid.GetHtml(
                columns: varGrid.Columns(
                    varGrid.Column("Name")

                ),
                displayHeader : false
        );
    }),
    grid.Column("Resource", format: (item) =>
    {
        WebGrid resGrid = new WebGrid(item.Resource);
        return resGrid.GetHtml(
            columns: resGrid.Columns(
                resGrid.Column("Name"),
                resGrid.Column("Parameters", format: (item) =>
                {
                    WebGrid resParamGrid = new WebGrid(item.Resource.Parameter);
                    return resParamGrid.GetHtml(
                    columns: resParamGrid.Columns(
                            resParamGrid.Column("Name")
                            ),
                            displayHeader: false
                        );
                })
            ),
            displayHeader: false
        );
    }),
    grid.Column("Parameter", format: (item) =>
    {
        WebGrid parGrid = new WebGrid(item.Parameter);
        return parGrid.GetHtml(
            columns: parGrid.Columns(
                    parGrid.Column("Name")
            ),
            displayHeader: false
        );
    })
)
)

So the grid has another grid, Resources. Every Resources grid has a grid containing the Parameters.

In VS there is no error, but when I try to run the application, it return as error:

"Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func, string, bool)' has some invalid arguments"

With a pointer to this spot:

grid.Column("Resource", format: (item) =>

Is it not possible to have recursive nested webgrids, or am I doing something wrong?


Solution

  • It's a problem with you repeating the item variable twice. Try renaming it:

    grid.Column("Resource", format: (item) =>
    {
        WebGrid resGrid = new WebGrid(item.Resource);
        return resGrid.GetHtml(
            columns: resGrid.Columns(
                resGrid.Column("Name"),
                resGrid.Column("Parameters", format: (resource) =>
                {
                    WebGrid resParamGrid = new WebGrid(resource.Parameter);
                    return resParamGrid.GetHtml(
                        columns: resParamGrid.Columns(
                            resParamGrid.Column("Name")
                        ),
                        displayHeader: false
                    );
                })
            ),
            displayHeader: false
        );
    })