I have the following code that works:
@for (var index = 0; index < Model.AdminSummaries.Count(); index++) {
<div class="rep_td0" id="rk_@(index)">
@Model.AdminSummaries[index].RowKey
</div>
<div class="rep_td0">
@Html.DropDownListFor(
x => x.AdminSummaries[index].Status,
new SelectList(
AdminStatusReference.GetAdminStatusOptions(),
"Value",
"Text",
),
new { id = string.Format("Status_{0}", index) }
)
I iterate through Model.AdminSummaries which is a type of
public IList<AdminSummary> AdminSummaries { get; set; }
As it is an IList I can use the indexing and x => x.AdminSummaries[index].Status works to get the correct status for each row and there’s also an index number that I can use to change the id of items for each row.
Now I have a problem. I want to apply the same code method to the following which is a class in my model with similar fields to AdminSummaries (including a Status field):
public ICollection<Item> Items { get; set; }
As it is an ICollection I think I cannot access elements using the [index]
I can use a foreach but HOW can I supply the status value to the first parameter of DropDownListFor? Also how can I get an index value so I can index the fields in the rows.
Update
The index point was something I added to the original question. Important that the solution can do something like the following so I can use jQuery on form fields later.
id="rk_@(index)"
You could use an editor template. So you replace this for loop with the following:
and then you define the editor template which will be rendered automatically for each element of the collection (
~/Views/Shared/EditorTemplates/Item.cshtml):