【问题标题】:Using for loop in a for loop to generate rows with content在 for 循环中使用 for 循环生成包含内容的行
【发布时间】:2020-02-03 01:26:16
【问题描述】:

我正在尝试生成如下视图

行数是在我的服务器端代码中预先确定的。我目前正在努力访问我列表中项目的数据。

<div class = "row">
 <div class = "col-12" >
 @for(int i = 1; i <= Model.RowCount; i++) {
  <div class = "row" >
   @for(int j = 0; j < Model.List.Count; j++) {
    <div class = "col-3 p-3 mb-4 ml-4 bg-dark text-white rounded">
     <b > @Model.List[j].Value < /b> 
     </div>
   } 
   </div>
 } 
  </div>
</div>

我真正想做的是将我的列表分成三部分,每行生成三个项目。在一行被内容填充后,它应该移动到下一行并继续生成内容,而不会丢失它访问的最后一个项目的跟踪。

例如:服务器生成第 1 行的最后一项并移动到第 2 行。这意味着第 1 行的最后一项是 list[2]。现在下一行的第一项应该是 list[3]。

感谢任何帮助!

【问题讨论】:

  • 你必须更具体。你得到什么样的错误?你得到什么样的输出?就目前而言,您可能会被否决

标签: c# asp.net-mvc for-loop asp.net-core


【解决方案1】:
<div class = "row">
 <div class = "col-12" >
 @var index = 0;
 @for(int i = 1; i <= Model.RowCount; i++) {
  <div class = "row" >
   @for(int j = 0; j < Model.List.Count; j++) {
    index++;
    <div class = "col-3 p-3 mb-4 ml-4 bg-dark text-white rounded">
     <b > @Model.List[index].Value < /b> 
     </div>
   } 
   </div>
 } 
  </div>
</div>

<div class = "row">
 <div class = "col-12" >
 @for(int i = 1; i <= Model.RowCount; i++) {
  <div class = "row" >
   @for(int j = 0; j < Model.List.Count; j++) {
    var index = Model.List.Count * (i-1) + j;
    <div class = "col-3 p-3 mb-4 ml-4 bg-dark text-white rounded">
     <b > @Model.List[index].Value < /b> 
     </div>
   } 
   </div>
 } 
  </div>
</div>

【讨论】:

  • 完美运行。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2021-10-28
  • 1970-01-01
  • 2015-03-21
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多