我正在尝试根据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:https://stackoverflow.com/a/24415952/2083526&#34; GridMvc和添加列时的if语句&#34;以及这一个gridmvc.codeplex.com
答案 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)