【问题标题】:GridMvc and if statement when adding columns添加列时的 GridMvc 和 if 语句
【发布时间】:2019-04-04 00:01:20
【问题描述】:

我正在使用 GridMvc,例如:

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

...

如何在此处使用 if 语句。我想创建 if 语句,例如:

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

但我不知道如何在 gridmvc 中创建 if 语句。

【问题讨论】:

  • 为什么不在 if 中添加一列?还是只是三级算子?

标签: c# asp.net-mvc-3


【解决方案1】:

你可以用剃须刀@helper 做类似的事情

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

然后在您的网格中看起来像

@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);
})

【讨论】:

    【解决方案2】:

    我认为这段代码有同样的效果

    @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);
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多