简体   繁体   English

如何在 asp.net mvc 中的表中创建一个计数器

[英]How to make a counter in a table in asp.net mvc

Hello, good afternoon.你好,下午好。 I am making a view in Razor ASP.NET 6. I find myself making a table and what I want to do is in the column N° add a counter, that for each row it creates, I add 1,2,3,4,5, etc. Try to do it with a for, foreach, but it is returning something that does not interest me, or it returns the total value of the rows.我在 Razor ASP.NET 中创建一个视图 6. 我发现自己创建了一个表,我想做的是在 N° 列中添加一个计数器,对于它创建的每一行,我添加 1,2,3,4,5等。尝试用 for、foreach 来做,但它返回的是我不感兴趣的东西,或者它返回行的总值。 And what I need is to add 1. I'm going to leave an example image.而我需要的是加 1。我将留下一个示例图像。 I would be very grateful if someone can help me.如果有人能帮助我,我将不胜感激。 Thanks.谢谢。 在此处输入图像描述

@model Taller.Model.Entities.Remito
@{
var counter = @Model.RemitoDetalle.Count();
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>

  </head>
 <body
style="height: 297mm; width: 210mm; margin-left: auto; margin-right: auto"
 >

  <!-- Tabla -->
  <div class="table">
    <table style="width: 100%">
      <tr>
        <th style="text-align: left">N°</th>
        <th>Detalle</th>
        <th style="text-align: left">Cantidad</th>
        <th style="text-align: left">Alto</th>
        <th style="text-align: left">Ancho</th>
      </tr>
      <tbody>
           @foreach (var item in Model.RemitoDetalle)
            {
        <tr>
            @for (var i = 1; i < counter; i++)
           {
               <td>@i</td>
           }
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Descripcion) 
          </td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Cantidad)</td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Alto)</td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Ancho)</td>
        </tr>
            }
      </tbody>
    </table>
  </div>

You're going over all possible values of the counter for every row.您将遍历每一行的计数器的所有可能值。 You should only increment it once per row, like this:您应该每行只增加一次,如下所示:

<tbody>
   @int i = 0;
   @foreach (var item in Model.RemitoDetalle)
    {
        i++;
        <tr>
          <td>@i</td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Descripcion) 
          </td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Cantidad)</td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Alto)</td>
          <td>@Html.DisplayFor(modelItem => item.PresupuestoDetalle.Ancho)</td>
        </tr>
    }
</tbody>

Try this:尝试这个:

<tbody>
    @for (var i = 0; i < Model.RemitoDetalle.Count(); i++)
    {
        <tr>
            <td>@i</td>
            <td>
                @Html.DisplayFor(model => model.RemitoDetalle[i].PresupuestoDetalle.Descripcion)
            </td>
            <td>@Html.DisplayFor(model => model.RemitoDetalle[i].PresupuestoDetalle.Cantidad)</td>
            <td>@Html.DisplayFor(model => model.RemitoDetalle[i].PresupuestoDetalle.Alto)</td>
            <td>@Html.DisplayFor(model => model.RemitoDetalle[i].PresupuestoDetalle.Ancho)</td>
        </tr>
    }
</tbody>

Replace代替

@for (var i = 1; i < counter; i++)
{
    <td>@i</td>
}    

by经过

<td>@(++counter)</td>

And on begin of the view or another code line before using it:在使用它之前,在视图或其他代码行的开头:

@{
    var counter = 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM