GridMvc 根据 if 条件渲染列

GridMvc render column based on if condition

我正在尝试根据 gridmvc 中的条件呈现列,如下所示:

@model IEnumerable< BasicSuprema.Models.BioUserModel >
@using GridMvc.Html

@helper CustomRenderingOfColumn(BasicSuprema.Models.BioUserModel users)
{
    if (users.cardType == 0)
    {
        <span class="label label-success">Normal</span>
    }
    else if(users.cardType == 1)
    {
        <span class="label label-important">ByPass</span>
    }
    else
    {

    }
}
  @Html.Grid(Model).Named("UsersGrid").Columns(columns =>
                    {
                        columns.Add(c => c.ID).Titled("ID");
                        columns.Add(c => c.cardType).Titled("Card Type")
                        .RenderValueAs(c => c.CustomRenderingOfColumn(c));

                    }).WithPaging(10).Sortable(true)

我遇到编译错误

'BasicSuprema.Models.BioUserModel' does not contain a definition for 'CustomRenderingOfColumn' and no extension method 'CustomRenderingOfColumn' accepting a first argument of type 'BasicSuprema.Models.BioUserModel' could be found 

这个错误在线.RenderValueAs(c => c.CustomRenderingOfColumn(c)); 我试过 SO

的解决方案

1: "GridMvc and if statement when adding columns" as well as this one gridmvc.codeplex.com

这将是一个迟到的答案,但我最近根据 if 条件使用了 RenderValueAs。在您的情况下,错误非常清楚,表明 BasicSuprema.Models.BioUserModel 没有方法定义为 CustomRenderingOfColumn

所以,你应该改变它;

.RenderValueAs(c => c.CustomRenderingOfColumn(c));

.RenderValueAs(c => CustomRenderingOfColumn(c));

因为 CustomRenderingOfColumn 不是您模型的一部分。 (BioUserModel)

完整的代码看起来像;

@Html.Grid(Model).Named("UsersGrid").Columns(columns =>
        {
            columns.Add(c => c.ID).Titled("ID");
            columns.Add(c => c.cardType).Titled("Card Type")
            .RenderValueAs(c => CustomRenderingOfColumn(c));

        }).WithPaging(10).Sortable(true)