1

Ao invés de exibir o valor de item.Endereco no WebGrid o que é exibido é um texto @item.Endereco.Length > 0 ? item.Endereco.Substring(0, item.Endereco.Length - 3) : item.Endereco.
No meu cenário atual tenho que fazer essa validação com o substring na View (no webgrid)

@grid.GetHtml(
    tableStyle: "grid",
    headerStyle: "head",
    columns: grid.Columns(
        grid.Column("Empresa", "Empresa"),
        grid.Column("Nome", format: @<text><a href="@Url.Action("Index", "Home", new { id = item.Id})">@item.Nome</a></text>),
        grid.Column("Endereco", format: @<text><a href="@Url.Action("Index", "Contato", new { id = item.Id})">@item.Endereco.Length > 0 ? item.Endereco.Substring(0, item.Endereco.Length - 3) : item.Endereco</a></text>)
    )
)

1 Resposta 1

1

Quando for usar o valor da variavel na view tem que por o @ caso contrario será interpretado como texto.

grid.Column("Endereco", format: @<text><a href="@Url.Action("Index", "Contato", new { id = item.Id})">@item.Endereco.Length > 0 ? @item.Endereco.Substring(0, @item.Endereco.Length - 3) : @item.Endereco</a></text>)

- Outra solução possivel:

@(item.Endereco.Length > 0 ? item.Endereco.Substring(0, item.Endereco.Length - 3) : item.Endereco)

Utilizar @(codigo aqui)

- No seu exemplo:

grid.Column("Endereco", format: @<text><a href="@Url.Action("Index", "Contato", new { id = item.Id})">@(item.Endereco.Length > 0 ? item.Endereco.Substring(0, item.Endereco.Length - 3) : item.Endereco)</a></text>

1
  • Obrigado @MeAudaSilvio funcionou corretamente, porém tenho uma outra situação parecida como esta: a formatação dentro de uma condição if no Html.Row aqui (pt.stackoverflow.com/questions/212730/…) eu agradeço.
    – hard123
    14/06/2017 às 10:43

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .