How to put html id tag with variable name using Razor

Asked

Viewed 338 times

2

Hey, you guys.

A very silly doubt, but it’s giving me a lot of headache...

I have this snippet of html code using Razor in Asp net mvc, I want to merge the id of this snippet, like, "name + code that varies", how do I do? I know it’s simple, but I’m not getting it...

 <td id='permiteCertificacao + "@relatorioDTO.Identificador"'>@relatorioDTO.Nome</td>

Thank you!

  • 1

    have you tried removing double quotes from your c#? For example: <td id='[email protected]'>@relatorioDTO.Nome</td>

  • 1

    Tried to use the @Html.Raw()?

  • used and it worked!! I will update my post to suddenly help someone. Thank you.

  • solved in this way: @Html.Raw("<td class='largura20' id='permiteCertificacao" + @relatorioDTO.Identificador + "'>" + @relatorioDTO.Nome + "</td>");

2 answers

1

Basically in your view you can do: <td id="[email protected]">

Follow an example

Model

public class Fruta
{
    public int FrutaId { get; set; }
    public string NomeFruta { get; set; }
}

Controller

 public IActionResult Index()
    {
        var listaFruta = new List<Fruta>
        {
            new Fruta { FrutaId = 1, NomeFruta = "Pera" },
            new Fruta { FrutaId = 2, NomeFruta = "Uva" },
            new Fruta { FrutaId = 3, NomeFruta = "Maçã" },
            new Fruta { FrutaId = 4, NomeFruta = "Salada Mista" }
        };

        return View(listaFruta);
    }

View

@model IEnumerable<WebApplication1.Models.Fruta>

@{
    ViewData["Title"] = "Index";
}

 <h2>Index</h2>

 <p>
  <a asp-action="Create">Create New</a>
 </p>
 <table class="table">
    <thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FrutaId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.NomeFruta)
        </th>
        <th></th>
    </tr>
</thead>
<tbody>
 @foreach (var item in Model) {
    <tr>
        <td id="[email protected]">
            @Html.DisplayFor(modelItem => item.FrutaId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NomeFruta)
        </td>
    </tr>

}

1


It’s actually really quite simple:

<td id='@string.Format("permiteCertificacao{0}", relatorioDTO.Identificador)'>@relatorioDTO.Nome</td>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.