【问题标题】:Razor Nested WebGrid剃刀嵌套 WebGrid
【发布时间】:2011-04-20 15:20:13
【问题描述】:

如何嵌套 WebGrid,每列都有很多格式。我可以做一个嵌套的for循环,但我基本上需要它来进行分页。或者还有其他更好的选择吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 razor paging webgrid


    【解决方案1】:

    请原谅冗长的数据设置,但这有效...

    @{
        var data = Enumerable.Range(0, 10).Select(i => new { Index = i, SubItems = new object[] { new { A = "A" + i, B = "B" + (i * i) } } }).ToArray();
        WebGrid topGrid = new WebGrid(data);
    }
    
    @topGrid.GetHtml(columns:
        topGrid.Columns(
            topGrid.Column("Index"),
            topGrid.Column("SubItems", format: (item) =>
            {
                WebGrid subGrid = subGrid = new WebGrid(item.SubItems);
                return subGrid.GetHtml(
                        columns: subGrid.Columns(
                            subGrid.Column("A"),
                            subGrid.Column("B")
                        )
                    );
            })
        )
    )
    

    渲染:

    当然,您必须确保在 GetHtml() 方法调用中为每个网格(顶部和子)指定唯一的参数名称以进行分页/排序,否则最终会出现冲突。

    【讨论】:

    • +1 我只需要对外部网格进行分页。内部网格最多有 5 到 7 行,我可能不会超过一列。所以对于嵌套网格,我打算有一个长列,其中包含所有模型字段的格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2011-05-10
    • 2015-04-22
    • 2021-02-27
    相关资源
    最近更新 更多