Assign a counter as Id to an HTML tag

1

My question is the following, in my project of ASP.NET , I have in a view, the foreach of razor with which I print my values of the model, and a div is created for each iteration, what I need is that this div have an id autoincrementable , due to the logic of some functions of JS that I have.

How can I achieve this?

Since if I assign a variable automatically it transforms the character into a string and does not take the numerical value.

@model IEnumerable<MvcFrontBolsaTrabajo.Models.ParaProcReportes.PostulacionesXCandidatoModel>
@using Newtonsoft.Json;

@{
    Layout = "~/Views/Shared/_LayoutCandidato.cshtml";
}


@{
    ViewBag.Title = "PostulacionesXCandidato";
}
    <div id="divP">

        @foreach (var item in Model)
        {
                    <div class="panel panel-primary " id=""  style="display:inline-block; text-align:center; width: 16%; margin: 5%;">
                    <div class="panel-heading">
                        @Html.DisplayFor(modelItem => item.TituloVacante)
                    </div>
                    <p>
                        @Html.DisplayFor(modelItem => item.Nombresoc)
                    </p>
                    <p>
                        @Html.DisplayFor(modelItem => item.Nombreciu)
                    </p>
                    <p>
                        @Html.DisplayFor(modelItem => item.FechaPostulacionvc)
                    </p>
                    <div>

                    </div>
                </div>

        }
    </div>
    
asked by Roberto Dominguez 20.06.2017 в 20:41
source

1 answer

1

You declare the variable in your view Razor before foreach

@{int contador= 1; } 

And in its foreach , it would print the value of the variable in id of div and in the end it would increase.

 @foreach (var item in Model)
  {
   <div class="panel panel-primary " id="@contador" 
    style="display:inline-block; text-align:center; width: 16%; margin: 5%;">
     /* Demás columnas*/
     //Incrementamos el Valor
      contador = contador + 1;
 }
  

Reference

    
answered by 20.06.2017 / 20:55
source