The contents of the variable are not displayed in View

1

Instead of displaying the value of item.Endereco in the WebGrid what is displayed is a text @ item.Endereco.Length > 0? item.Endereco.Substring (0, item.Endereco.Length - 3): item.Endereco . Home In my current scenario I have to do this validation with substring in View (in 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>)
    )
)
    
asked by anonymous 13.06.2017 / 23:36

1 answer

1

When you use the value of variavel in view you have to @ otherwise it will be interpreted as 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>)

- Another possible solution:

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

Use @(codigo aqui)

- In your example:

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>

    
13.06.2017 / 23:57