I have created a ViewModel which I am using as the model for my view. Within the view model it contains the following definition:
public class ItemDetailViewModel
{
public int ItemId { get; set; }
public bool Active { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<ItemDetailItemOptionViewModel> ItemOptions { get; set; }
}
As you can see it contains a List<> named ItemOptions. My view model is tied to ItemDetailViewModel. Within the view I loop through each element in the list and output them:
@model app1.Models.ViewModels.ItemDetailViewModel
@{
ViewBag.Title = "Item " + @Html.DisplayFor(model => model.ItemId);
}
//...
<thead>
<tr>
<th>Active</th>
<th>Names</th>
</tr>
</thead>
<tbody>
@foreach (app1.Models.ViewModels.ItemDetailItemOptionViewModel ItemOption in Model.ItemOptions)
{
<tr>
<td>@ItemOption.Active</td>
<td>@ItemOption.Name</td>
</tr>
}
</tbody>
//...
What I want to be able to do is instead of hard coding the name and values, I want to be able to user the helpers I use else where within the page, such as:
@Html.DisplayNameFor(model => model.ItemCode)
@Html.DisplayFor(model => model.ItemCode)
I've tried the following
@Html.DisplayNameFor(model => model.ItemOptions.ItemCode)
@Html.DisplayFor(model => model.ItemOptions.ItemCode)
But this gives the following error:
Error 1 'System.Collections.Generic.List<[].Models.ViewModels.ItemDetailItemOptionViewModel>' does not contain a definition for 'ItemCode' and no extension method 'ItemCode' accepting a first argument of type 'System.Collections.Generic.List<[].Models.ViewModels.ItemDetailItemOptionViewModel>' could be found (are you missing a using directive or an assembly reference?) ....\Views\Item\Details.cshtml 87 81 []
I've also tried doing it off the ItemOption object which I loop on but get similar issues. What am I doing wrong?