从System.Web.Helpers.WebGrid中渲染部分视图

时间:2011-05-07 00:34:41

标签: asp.net-mvc-3

我正在尝试从System.Web.Helpers.WebGrid

中渲染Partial

我的模型类看起来像这样:

class GameInfo
{
    public List<AppUser> Team1 { get; set; }
        public List<AppUser> Team2 { get; set; }
    // and more properties
}

class AppUser
{
        public string PictureUrl { get; set; }
        public string ProfileUrl { get; set; }
        public long GamesWon { get; set; }
        public long GamesLost { get; set; }
        public int Points { get; set; }
    // and more properties
}

我希望我的GridView在我的网格视图中显示GameInfo的列表。 结果是比预期更难的是渲染团队(List)。 为了保持干燥,我创建了一个部分视图来渲染一个团队(_Team.cstml)。

这是我的剃刀代码:

@if (Model != null)
{
   var webgrid = new WebGrid(source: Model.Games,
   rowsPerPage: 10);


<div id="grid">
    @webgrid.GetHtml(
    columns: webgrid.Columns(           
         webgrid.Column(header: "Score", format: @<text>@item.Score1/@item.Score1</text>),
         webgrid.Column(header: "Team 1", format: (item) =>
            {

                return "hello sb"; // this line works!
                //return Html.Partial("_Team", item.Team1);  // this gives an error
            })
         )
         )
</div>
}

知道如何才能让它发挥作用吗?

谢谢!

1 个答案:

答案 0 :(得分:8)

如果遇到其他人,我今天早上设法解决了这个问题。 这有效:

webgrid.Column(header: "Team 1", format: (item) =>
                {
                    List<Cuarenta.Web.Models.AppUser> team = ((Cards.Cloud.WebRole.Admin.GameInfo)item.Value).Team1;                       
                    return Html.Partial("_Team", team);
                })