I have an MVC View and controller and in order to be able to pull out the correct information to display on the view I have used a linq query and converted this to a list to be picked up by the view, but since I changed the type to List from IEnumerable I an unable to get the Display names of the properties using @Html.DisplayNameFor(model => model.Name).
View Code:
@model List<UKAIS.Common.Brands.Data.Opex>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameForModel()
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.BulletPoints)
</th>
<th>
@Html.DisplayNameFor(model => model.ImageUrl)
</th>
<th>
@Html.DisplayNameFor(model => model.PolicyWordingLink)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.OpexType)
</th>
<th>
@Html.DisplayNameFor(model => model.SortOrder)
</th>
<th>
@Html.DisplayNameFor(model => model.MustSelectOption)
</th>
<th>
@Html.DisplayNameFor(model => model.Selected)
</th>
<th>
@Html.DisplayNameFor(model => model.DateCreated)
</th>
<th>
@Html.DisplayNameFor(model => model.DateModified)
</th>
<th>
@Html.DisplayNameFor(model => model.ModifiedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.RowVers)
</th>
<th>
@Html.DisplayNameFor(model => model.Active)
</th>
<th>
@Html.DisplayNameFor(model => model.SubHeading)
</th>
<th>
@Html.DisplayNameFor(model => model.VariantKeyText)
</th>
<th>
@Html.DisplayNameFor(model => model.VariantOptionText)
</th>
<th>
@Html.DisplayNameFor(model => model.OpexGroup.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.BulletPoints)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImageUrl)
</td>
<td>
@Html.DisplayFor(modelItem => item.PolicyWordingLink)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.OpexType)
</td>
<td>
@Html.DisplayFor(modelItem => item.SortOrder)
</td>
<td>
@Html.DisplayFor(modelItem => item.MustSelectOption)
</td>
<td>
@Html.DisplayFor(modelItem => item.Selected)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateCreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateModified)
</td>
<td>
@Html.DisplayFor(modelItem => item.ModifiedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.RowVers)
</td>
<td>
@Html.DisplayFor(modelItem => item.Active)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubHeading)
</td>
<td>
@Html.DisplayFor(modelItem => item.VariantKeyText)
</td>
<td>
@Html.DisplayFor(modelItem => item.VariantOptionText)
</td>
<td>
@Html.DisplayFor(modelItem => item.OpexGroup.Name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Controller Code:
//Returns Opex list dependant on brand
public async Task<ActionResult> Index(Guid? id)
{
if (id == null)
{
var opexes = db.Opexes.Include(o => o.OpexGroup).ToList();
return View(opexes);
}
else
{
var opexes = (from o in db.Opexes
join b in db.BrandOpexes on o.Id equals b.OpexId
where b.BrandId == id
select new Opex()
{
Active = o.Active,
Code = o.Code,
BulletPoints = o.BulletPoints,
ImageUrl = o.ImageUrl,
PolicyWordingLink = o.PolicyWordingLink,
Description = o.Description,
Price = o.Price,
OpexType = o.OpexType,
SortOrder = o.SortOrder,
MustSelectOption = o.MustSelectOption,
Selected = o.Selected,
DateCreated = o.DateCreated,
DateModified = o.DateModified,
ModifiedBy = o.ModifiedBy,
RowVers = o.RowVers,
SubHeading = o.SubHeading,
VariantKeyText = o.VariantKeyText,
VariantOptionText = o.VariantOptionText,
}).ToList();
return View(opexes);
}
}