我有一个MVC视图和控制器,为了能够提取正确的信息以显示在视图上,我使用了linq查询并将其转换为列表以供视图使用,但是由于更改了类型从IEnumerable列出,我无法使用@ Html.DisplayNameFor(model => model.Name)获取属性的显示名称。
查看代码:
@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>
控制器代码:
//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);
}
}
最佳答案
您在模型中使用List<UKAIS.Common.Brands.Data.Opex>
,但您使用的模型类似于DisplayNameFor
中的键入(DisplayName对于不知道要显示哪个元素)
更改此:
@Html.DisplayNameFor(model => model.Name)
为此(类似这样)
@Html.DisplayNameFor(model => model[0].Name)
最好的方法是使用模型中的一项创建变量
var displayItem = model[2];
@Html.DisplayNameFor(displayItem => displayItem.Name)
帮助链接:
DisplayNameFor() From List<Object> in Model