senzacionale
senzacionale

Reputation: 20916

GridMvc and if statement when adding columns

I am using GridMvc like:

@Html.Grid(Model.Customers).Columns(columns =>
            {
columns.Add(x => x.FirstName).Titled(Translations.Global.FIRST_NAME).SetWidth(110).Sortable(true);

...

How can I use if statement here. I woul dlike to create if statement, like:

if (x.LastName == 'Me')
{
<span class="label label-success">Active</span>
}
else
{
<span class="label label-important">Banned</span>
}

but I have no idea how to creater if statement in gridmvc.

Upvotes: 2

Views: 7370

Answers (2)

PaulBinder
PaulBinder

Reputation: 2052

Coud you use a razor @helper and do something like

@helper CustomRenderingOfColumn(Customer customer)
{
    if (customer.LastName == 'Me')
    {
    <span class="label label-success">Active</span>
    }
    else
    {
    <span class="label label-important">Banned</span>
    }
}

then in your grid would look something like

@Html.Grid(Model).Columns(columns =>
{
        columns.Add(o => o.Customer.IsVip)
                .Titled("Vip customer")
columns.Add(x=>x.FirstName)
.Titled(Translations.Global.FIRST_NAME)
.SetWidth(110)
.RenderValueAs(o => CustomRenderingOfColumn(o))
.Sortable(true);
})

Upvotes: 8

flpdev
flpdev

Reputation: 86

I think this code had the same effect

@Html.Grid(Model).Columns(columns =>
{
    columns.Add(o => o.Customer.IsVip).Titled("Vip customer")
    columns.Add()
           .Titled(Translations.Global.FIRST_NAME)
           .SetWidth(110)
           .Encoded(false)
           .RenderValueAs(o => 

               @if (o.LastName == 'Me')
               {
                   <span class="label label-success">Active</span>
               }
               else
               {
                   <span class="label label-important">Banned</span>
               }
    )
    .Sortable(true);
})

Upvotes: 1

Related Questions