2

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);

    }

}
Simon Karlsson
  • 4,090
  • 22
  • 39
Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • http://stackoverflow.com/questions/20807869/displaynamefor-from-listobject-in-model – Ric Feb 01 '16 at 10:37
  • `DisplayNameFor()` is fine. Try commenting out the `@Html.DisplayNameForModel()` line of code. The other probable cause is `@Html.DisplayNameFor(model => model.OpexGroup.Name)` (your accessing a nested property in which case you need `.FirstOrDefault()` –  Feb 01 '16 at 10:45

2 Answers2

3

You are using List<UKAIS.Common.Brands.Data.Opex> in model, but You use this model like type in DisplayNameFor (DisplayNameFor not know which element You want to display)

Change this:

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

to this(something like this)

@Html.DisplayNameFor(model => model[0].Name)

Best way is to create variable with one item from model

var displayItem = model[2];

 @Html.DisplayNameFor(displayItem => displayItem.Name)

Help links:

DisplayNameFor() From List<Object> in Model

Community
  • 1
  • 1
blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22
  • This is not correct. `DisplayNameFor()` has an overload that accepts `IEnumerable`. Using `@Html.DisplayNameFor(model => model[0].Name)` is unnecessary and would throw an exception if the collection contained no items –  Feb 01 '16 at 10:44
  • Are You sure? Look this: http://stackoverflow.com/questions/20807869/displaynamefor-from-listobject-in-model – blogprogramisty.net Feb 01 '16 at 10:47
  • `@Html.DisplayNameFor(model => model.Name)` works fine - it just that razor initially complains. As soon as the code is run it works fine (at least in MVC-5) But I was wrong about throwing an exception (it uses the `ModelMetadata`) –  Feb 01 '16 at 10:56
0

You have to Use

@Html.DisplayNameFor(model => model.First().Name)
wertzui
  • 5,148
  • 3
  • 31
  • 51