Foreach剃须刀内的Foreach

编程入门 行业动态 更新时间:2024-10-12 05:46:41
本文介绍了Foreach剃须刀内的Foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试编写一个foreach循环,该循环将找到每种不同的类别类型,然后列出具有该类别分配的每个Title.

I am trying to write a foreach loop that will find each distinct category type, and then list each Title that has that category assignment.

例如:

@model IEnumerable<CMESurvey.Models.SurveyProgramModel> @{ ViewBag.Title = "Home"; } @foreach (var item in Model) { <h2>@Html.DisplayFor(modelItem => item.ProgramType.ProgramType)</h2> foreach (var listing in Model) { <ul> <li>@Html.DisplayFor(modelItem => listing.ProgramTitle)</li> </ul> } }

调查响应模型:

public class SurveyProgramModel { [Key] public int ProgramId { get; set; } public int ProgramYear { get; set; } public int ProgramStatusId { get; set; } public string ProgramTitle { get; set; } public virtual SurveyProgramModel SurveyProgramModel { get; set; } public virtual PersonModel PersonModel { get; set; }

}

我遇到了2个问题.

1.)我需要它仅显示每个类别一次,而不是列出每个项目实例的类别.

1.) I need it to only display each category once, rather than listing the category for each item instance.

2.)它显示所有ProgramTitle,而不仅仅是该循环的ProgramTitle.

2.) It is displaying all ProgramTitle, rather than just the the the ProgramTitle for that loop.

不确定我应该使用什么语法.

Not sure what the syntax I should be using is.

推荐答案

如果我正确理解,那应该是

If I understand correctly then it should be like

@foreach (var item in Model) { <h2>@Html.DisplayFor(modelItem => item.ProgramType.ProgramType)</h2> foreach (var listing in item.SurveyResponseModels) { <ul> <li>@Html.DisplayFor(modelItem => listing.ProgramTitle)</li> </ul> } }

更多推荐

Foreach剃须刀内的Foreach

本文发布于:2023-11-15 16:19:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剃须刀   Foreach

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!