我想按“TimeName”字段对返回的项目进行分组,所以我不想在每一行重复字段TimeName,代码如下:
@foreach (var item in Model) {
tim = item.MealTime;
@Html.DisplayFor(modelItem => item.TimeName)
<text> , </text>
@Html.DisplayFor(modelItem => item.MealTime)
<br />
@Html.DisplayFor(modelItem => item.FoodName)
@Html.DisplayFor(modelItem => item.Quantity)
@Html.DisplayFor(modelItem => item.FoodUnit)
@Html.DisplayFor(modelItem => item.MealDate)
@Html.DisplayFor(modelItem => item.MealTimeSpecific)
@Html.DisplayFor(modelItem => item.IsActive)
@Html.DisplayFor(modelItem => item.InsertDate)
@Html.DisplayFor(modelItem => item.InsertUser)
<br />
}
我试图制作嵌套for循环,但它不起作用,有没有办法解决这个问题?
答案 0 :(得分:1)
我找到了一个解决方案,但我希望能有更好的解决方案,
@{
tim = Model.FirstOrDefault().MealTime;
}
@Model.FirstOrDefault().TimeName
<br />
@foreach (var item in Model) {
if (tim != item.MealTime)
{
@Html.DisplayFor(modelItem => item.TimeName)
<br />
}
tim = item.MealTime;
@Html.DisplayFor(modelItem => item.MealTime)
@Html.DisplayFor(modelItem => item.FoodName)
@Html.DisplayFor(modelItem => item.Quantity)
@Html.DisplayFor(modelItem => item.FoodUnit)
@Html.DisplayFor(modelItem => item.MealDate)
@Html.DisplayFor(modelItem => item.MealTimeSpecific)
@Html.DisplayFor(modelItem => item.IsActive)
@Html.DisplayFor(modelItem => item.InsertDate)
@Html.DisplayFor(modelItem => item.InsertUser)
<br />
}