Reputation: 20916
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
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
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