5

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

4

1 回答 1

12

请原谅详细的数据设置,但这有效......

@{
    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() 方法调用中为每个网格(顶部和子)提供唯一的参数名称以进行分页/排序,否则最终会出现冲突。

于 2011-04-20T16:36:06.087 回答