有条件地向 WebGrid 添加一列

Conditionally add a column into WebGrid

1) 我正在尝试有条件地将一列添加到 WebGrid 中。它是一个锚标签(),里面有一个img标签()。我该怎么做?

2) 只有当2个参数不为null(byte[] item.Boleto, string item.Nome_Arquivo)时才会显示该列。如果没有通过 sql 查询选择值,我将它们的值都设置为 null。因此,我只需要检查两者是否不为空,然后显示该列。但同样,我如何检查它?我可以在 WebGrid 结构中的什么地方放置条件语句或类似的东西(例如三元等)而不破坏它的其余部分?

让我知道是否需要任何翻译才能理解我的问题

我想要 "translate" 到 WebGrid 模式的行:

grid.Column("", "", format:  @<a href="data:application/pdf;charset=utf-8;base64,@System.Convert.ToBase64String(item.Boleto).ToString()" target="_blank" download="@item.Nome_Arquivo" type="application/pdf" rel="noopener noreferrer"><img src="@Url.Content("~/Content/images/download_arquivo.png")" alt="down" height="16" width="16" /></a>)

这是完整的网格(不是必需的,但为了清楚起见,我给出了它):

@
@{   
@grid.GetHtml(
    alternatingRowStyle: "linha-Alternada",
    htmlAttributes: new { id = "gridListagemTituloReceber" },
    fillEmptyRows: true,
    columns: grid.Columns(


        grid.Column("", "", format:  @<a href="data:application/pdf;charset=utf-8;base64,@System.Convert.ToBase64String(item.Boleto).ToString()" target="_blank" download="@item.Nome_Arquivo" type="application/pdf" rel="noopener noreferrer"><img src="@Url.Content("~/Content/images/download_arquivo.png")" alt="down" height="16" width="16" /></a>),

        grid.Column("Tipo", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Tipo),
        grid.Column("Data_Emissao", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Data_Emissao,
            format: (item) => item.Data_Emissao == null ? "" : String.Format("{0:dd/MM/yyyy}", item.Data_Emissao)),
        grid.Column("Chave", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Chave),
        grid.Column("Numero_Titulo", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Numero_Titulo),
        grid.Column("Nota_Saida", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Nota_Saida),
        grid.Column("Data_Vencimento", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Data_Vencimento,
            format: (item) => item.Data_Vencimento == null ? "" : String.Format("{0:dd/MM/yyyy}", item.Data_Vencimento)),
        grid.Column("Valor", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Valor,
            format: (item) => item.Valor == null ? "" : String.Format("{0:N2}", item.Valor),
            style: "alinhado-Direita"),                
        grid.Column("Data_Baixa", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Data_Baixa,
            format: (item) => item.Data_Baixa == null ? "" : String.Format("{0:dd/MM/yyyy}", item.Data_Baixa)),
        grid.Column("Valor_Recebido", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Valor_Recebido,
            format: (item) => item.Valor_Recebido == null ? "" : String.Format("{0:N2}", item.Valor_Recebido),
            style: "alinhado-Direita"),
        grid.Column("Valor_Atualizado", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Valor_Atualizado,
            format: (item) => item.Valor_Atualizado == null ? "" : String.Format("{0:N2}", item.Valor_Atualizado),
            style: "alinhado-Direita"),
        grid.Column("Codigo_Especie", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Codigo_Especie),
        grid.Column("Descricao_Baixa", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Descricao_Baixa),
        grid.Column("Data_Devolucao", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Data_Devolucao,
            format: (item) => item.Data_Devolucao == null ? "" : String.Format("{0:dd/MM/yyyy}", item.Data_Devolucao)),
        grid.Column("Motivo_Devolucao", ModelNames.PedidoPerfilClienteAbaTituloReceberViewModel_Motivo_Devolucao)
    )
)

}

尝试

grid.Column("", "",
    @<text>
         @if (item.Boleto != null && item.Nome_Arquivo != null)
         {
            <a href="data:application/pdf;charset=utf-8;base64,@System.Convert.ToBase64String(item.Boleto).ToString()" target="_blank" download="@item.Nome_Arquivo" type="application/pdf" rel="noopener noreferrer"><img src="@Url.Content("~/Content/images/download_arquivo.png")" alt="down" height="16" width="16" /></a>
         }
     </text>),