Reputation: 14511
I have a foreach loop that builds hyperlinks based on the number of items in the model. It's working fine, except the part that says: ProgramId = @item.ProgramIds.First()
is only returning the first program ID for each ProgramType. What is the syntax for letting it loop through all of the program ID's rather than just the first?
@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; }
}
Controller action:
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);
}
Upvotes: 2
Views: 1887
Reputation: 7692
I'm a little bit confused with your model and query. As far as I can understand, there should be a relationship between program and type, which there isn't.
So, I'd strongly recommend you to review your viewmodel and its query.
However, see if this helps:
@foreach (var currentProgramTypeViewModel in Model)
{
foreach(string currentProgramType in currentProgramTypeViewModel.ProgramTypes)
{
<h2>@currentProgramType</h2>
<ul>
@for (int mProgramIndex = 0; mProgramIndex < currentProgramTypeViewModel.ProgramIds.Count(); mProgramIndex++)
{
var programTitle = currentProgramTypeViewModel.ProgramTitles.ToList<string>()[mProgramIndex];
var programId = currentProgramTypeViewModel.ProgramIds.ToList<int>()[mProgramIndex];
<li>
@Html.ActionLink(programTitle, "Results", "SurveyResponse", new { ProgramId = programId }, null)
</li>
}
</ul>
}
}
Upvotes: 1
Reputation: 1550
item
refers to the value from the outer loop. It looks like you want to use the value from the inner loop:
@foreach (var ProgramTitles in item.ProgramTitles)
{
foreach(var programId in item.ProgramIds)
{
<li>
@Html.ActionLink(ProgramTitles, "Results", "SurveyResponse", new { ProgramId = programId }, null)
</li>
}
}
After you supplied the viewModel, it looks like you have ProgramTitles and ProgramIds as two disconnected arrays. Maybe, what you have is that an item at array index n for ProgramTitle has a corresponding entry in the nth item of the ProgramIds array. The viewModel can be improved, but this is my solution to that:
@for (var index = 0; index < item.ProgramTitles.Count; index++)
{
<li>
@Html.ActionLink(ProgramTitles[index], "Results", "SurveyResponse", new { ProgramId = item.ProgramIds[index]}, null)
</li>
}
Upvotes: 3