我有以下代码,无论我尝试过什么,我都无法让它只返回一次数据。
<div class="container">
@for (int i = 0; i < Model.First().NumberOfRows; i++)
{
<div class="row">
@foreach (var columns in Model)
{
<div class="@columns.BootstrapColumns">
@columns.BodyText
</div>
}
</div>
}
</div>
我已经尝试在代码中添加一个计数器,以便在线阅读帖子,但没有任何作用。
我错过了一些简单的东西,for循环工作正常,但是foreach导致问题。
将foreach移到for循环之外,它运行正常,但我需要它在for循环中。
<div class="container">
<div class="row">
<div class="col-md-3">
<p>column 1</p>
</div>
<div class="col-md-3">
<p>column 2</p>
</div>
<div class="col-md-3">
<p>column 3</p>
</div>
<div class="col-md-3">
<p>column 4</p>
</div>
<div class="col-md-4">
<p>Row 2 column 1</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 2</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 3</p>
</div>
<div class="col-md-12">
<p>Rows 3 column 1</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>column 1</p>
</div>
<div class="col-md-3">
<p>column 2</p>
</div>
<div class="col-md-3">
<p>column 3</p>
</div>
<div class="col-md-3">
<p>column 4</p>
</div>
<div class="col-md-4">
<p>Row 2 column 1</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 2</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 3</p>
</div>
<div class="col-md-12">
<p>Rows 3 column 1</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>column 1</p>
</div>
<div class="col-md-3">
<p>column 2</p>
</div>
<div class="col-md-3">
<p>column 3</p>
</div>
<div class="col-md-3">
<p>column 4</p>
</div>
<div class="col-md-4">
<p>Row 2 column 1</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 2</p>
</div>
<div class="col-md-4">
<p>Row 2 Column 3</p>
</div>
<div class="col-md-12">
<p>Rows 3 column 1</p>
</div>
</div>
Camilo Terevinto如果我这样做,我会得到以下哪些不是我&#39; M之后
<div class="container">
<div class="row">
<div class="col-md-3">
<p>column 1</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>column 2</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>column 3</p>
</div>
</div>
<div class="row">
<div class="col-md-3">
<p>column 4</p>
</div>
</div>
<div class="row">
<div class="col-md-4">
<p>Row 2 column 1</p>
</div>
</div>
<div class="row">
<div class="col-md-4">
<p>Row 2 Column 2</p>
</div>
</div>
<div class="row">
<div class="col-md-4">
<p>Row 2 Column 3</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>Rows 3 column 1</p>
</div>
</div>
</div>
您好user7396598我试图让代码显示以下内容:
<div class="row">
<div class="col-md-4">row 1 col1</div>
<div class="col-md-4">row 1 col2</div>
<div class="col-md-4">row 1 col3</div>
</div>
<div class="row">
<div class="col-md-3">row 2 col1</div>
<div class="col-md-3">row 2 col2</div>
<div class="col-md-3">row 2 col3</div>
<div class="col-md-3">row 2 col4</div>
</div>
<div class="row">
<div class="col-md-12">row 3 col1</div>
</div>
我的for循环获取行数,然后foreach循环应该获取列并显示在行的列中。
如果没有先获得带有for循环的行数,我看不到任何其他方法。
======================查看模型====================
public class NestedContentViewModel
{
public NestedContentViewModel() { }
public NestedContentViewModel(int numberOfRows, HtmlString bodyText, string image, string imageAlternativeText,string rowBackgroundColour, string bootstrapColumns, int numberOfColumns)
{
NumberOfRows = numberOfRows;
BodyText = bodyText;
Image = image;
ImageAlternativeText = imageAlternativeText;
RowBackgroundColour = rowBackgroundColour;
BootstrapColumns = bootstrapColumns;
NumberOfColumns = numberOfColumns;
}
public int NumberOfRows { get; set; }
public int NumberOfColumns { get; set; }
public string BootstrapColumns { get; set; }
public HtmlString BodyText { get; set; }
public string Image { get; set; }
public string ImageAlternativeText { get; set; }
public string RowBackgroundColour { get; set; }
}
==================控制器=====
foreach (var rows in nestedContentRows)
{
int numberOfBootstrapColumns = rows.GetPropertyValue<IEnumerable<IPublishedContent>>("columns").Count();
string bootstrapColumn = "";
switch (numberOfBootstrapColumns)
{
case 1:
bootstrapColumn = "col-md-12";
break;
case 2:
bootstrapColumn = "col-md-6";
break;
case 3:
bootstrapColumn = "col-md-4";
break;
case 4:
bootstrapColumn = "col-md-3";
break;
default:
bootstrapColumn = "col-md-12";
break;
}
foreach (var columns in rows.GetPropertyValue<IEnumerable<IPublishedContent>>("columns"))
{
HtmlString bodyText = columns.HasValue("bodyText")? columns.GetPropertyValue<HtmlString>("bodyText"): new HtmlString(string.Empty);
model.Add(new NestedContentViewModel(numberOfRows: countNumberOfRows,bodyText: bodyText, image: null, imageAlternativeText: null,rowBackgroundColour: "cssTurquoiseBlue", bootstrapColumns: bootstrapColumn, numberOfColumns: numberOfBootstrapColumns));
}
}
答案 0 :(得分:0)
所以,问题在于你的模型结构。和命名约定,你的名字引起各种混乱。目前,您的模型是NestedContentViewModel对象的列表,您正在为每个BodyText值创建一个新的NestedContentViewModel。
您的BodyText实际上应该是每个NestedContentViewModel中的集合。这将&#34;关联&#34;您的BodyText(列信息)到NestedContentViewModel(行)。另外,将rowNumber移出NestedContentViewModel并将其放在包含List的父级中
然后你的循环看起来像这样:
@foreach (var row in Model)
{
<div class="row">
@foreach (var column in row.ColumnCollection)
{
<div class="@row.BootstrapColumns">
@column.BodyText
</div>
}
</div>
}
我希望这能让你朝着正确的方向前进。如果您需要帮助重构模型,可能需要编辑并清理问题文本。