Reputation: 1034
I am trying to render a column based on condition in gridmvc like below :
@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)
I get a compilation error
'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
This error is on the line .RenderValueAs(c => c.CustomRenderingOfColumn(c));
I have tried the solution from SO
1: https://stackoverflow.com/a/24415952/2083526 "GridMvc and if statement when adding columns" as well as this one gridmvc.codeplex.com
Upvotes: 3
Views: 4729
Reputation: 13146
It will be a late answer but I have recently used RenderValueAs
based on if condition. In your case, the error is pretty clear that tells there is no method definition for BasicSuprema.Models.BioUserModel
as CustomRenderingOfColumn
.
So, you should change it;
.RenderValueAs(c => c.CustomRenderingOfColumn(c));
to
.RenderValueAs(c => CustomRenderingOfColumn(c));
Because CustomRenderingOfColumn
is not part of your model. (BioUserModel
)
Complete code looks like;
@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)
Upvotes: 3