Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridMvc render column based on if condition

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

like image 560
GotaloveCode Avatar asked Oct 24 '25 13:10

GotaloveCode


1 Answers

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)
like image 57
lucky Avatar answered Oct 27 '25 01:10

lucky