It looks like you need to group your enum using ProgramType in your controller. This will keep the view beautiful and clean.
IEnumerable<CMESurvey.Models.SurveyProgramModel> models = Foo();
Then your opinion is much simpler. (After you correct the type of your model).
@for(int i = 0; i < Model.Count; i++) { <h2>@Html.DisplayFor(model => model[i].Key)</h2> <ul> for(int j = 0; j < Model[i].Count; j++) { <li>@Html.DisplayFor(model => model[i][j].ProgramTitle)</li> } </ul> }
Alternative:
Or you make a list of lists:
var models = Foo(); var grouped = models.GroupBy(s => s.ProgramType.ProgramType) .Select(x => x.Select(y => y)) .ToList(); return View(grouped);
Then your view will change slightly:
@for(int i = 0; i < Model.Count; i++) { <h2>@Html.DisplayFor(model => model[i].First().ProgramType.ProgramType)</h2> <ul> for(int j = 0; j < Model[i].Count; j++) { <li>@Html.DisplayFor(model => model[i][j].ProgramTitle)</li> } </ul> }
source share