I need to access a specific item from IEnumerable @model ;
I know that I can reach all item by below code:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FoodName)
</td>
}
But I need to access the third item (as an example) by doing somthing like that :
Model[3].FoodName
Are there are any way to reach a specific index item from IEnumerable object in MVC ?
You could use the
ElementAt(index)extension method:Or change the model type from
IEnumerable<T>toIList<T>orT[]:now you could loop with an index:
This is the preferred approach if you generate input fields (EditorFor, TextBoxFor, …) because this way correct names will be assigned to them.