GridMvc基于if条件呈现列

时间:2015-03-12 06:53:26

标签: c# asp.net-mvc-4 gridview

我正在尝试根据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

的解决方案

1https://stackoverflow.com/a/24415952/2083526&#34; GridMvc和添加列时的if语句&#34;以及这一个gridmvc.codeplex.com

1 个答案:

答案 0 :(得分:1)

这将是一个迟到的答案,但我最近根据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)