无法从使用

时间:2016-02-01 10:31:59

标签: c# asp.net-mvc linq

我有一个MVC视图和控制器,为了能够提取正确的信息显示在视图上我使用了linq查询并将其转换为一个列表以供视图选取,但是因为我从IEnumerable将类型更改为List我无法使用@ 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);

    }

}

2 个答案:

答案 0 :(得分:3)

您在模型中使用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

答案 1 :(得分:0)

你必须使用

@Html.DisplayNameFor(model => model.First().Name)