我有一个 foreach 循环,它根据模型中的项目数构建超链接。它工作正常,除了说的部分:ProgramId = @item.ProgramIds.First()
只返回每个 ProgramType 的第一个程序 ID。让它遍历所有程序 ID 而不仅仅是第一个的语法是什么?
@model IEnumerable<CMESurvey.ViewModels.ProgramTypeViewModel>
@{
ViewBag.Title = "Home";
}
@foreach (var item in Model)
{
<h2>@Html.DisplayFor(modelItem => item.ProgramTypes)</h2>
<ul>
@foreach (var ProgramTitles in item.ProgramTitles)
{
<li>
@Html.ActionLink(@ProgramTitles, "Results", "SurveyResponse", new { ProgramId = @item.ProgramIds.First() }, null)
</li>
}
</ul>
}
public class ProgramTypeViewModel
{
public int ProgramTypeIds { get; set; }
public IEnumerable<string> ProgramTitles { get; set; }
public IEnumerable<int> ProgramIds { get; set; }
public IEnumerable<string> ProgramTypes { get; set; }
}
控制器动作:
public ViewResult Home()
{
var data = from SurveyProgramModel in surveyProgramRepository.Get()
group SurveyProgramModel by SurveyProgramModel.ProgramTypeId into programTypeGroup
select new ProgramTypeViewModel()
{
ProgramTypeIds = programTypeGroup.Key,
ProgramIds = programTypeGroup.Select(r => r.ProgramId),
ProgramTitles = programTypeGroup.Select(r => r.ProgramTitle),
ProgramTypes = programTypeGroup.Select(r => r.ProgramType.ProgramType).Distinct(),
};
return View(data);
}