I am trying to resolve the error 'List ActiveOrder02 does not contain a definition for Client' that appears when I put the mouse over '.Client' on the line @Html.DisplayNameFor(model => model.ActiveOrders02.Client) in the following View:
@model MVCDemo2.ViewModels.Order02VM
<table class="table">
<tr><th>@Html.DisplayNameFor(model => model.ActiveOrders02.Client)</th></tr>
@foreach (var item in Model.ActiveOrders02)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.Client)</td>
</tr>
}
For info here are the Controller and the ViewModel:
using MVCDemo2.Models;
using MVCDemo2.ViewModels;
using System.Linq;
using System.Web.Mvc;
namespace MVCDemo2.Controllers
{
public class Order02Controller : Controller
{
private InventoryContainer db02 = new InventoryContainer();
// GET: Order02
public ActionResult Index()
{
//Create instance of the (View)Model
var o = new Order02VM();
//Retrieves DATA from the DB
var resultSet02 = db02.SampleDbTable;
// Push the retrieved DATA into the (View)Model
o.ActiveOrders02 = resultSet02.Select(x => new ActiveOrder02
{
ID = x.tableId,
Client = x.tableClient
}).ToList();
//EDIT caused by answer of @David
return View(o);
//IT WAS: return View(o.ActiveOrders02.ToList());
}
}
}
Here follows the ViewModel:
using System.Collections.Generic;
namespace MVCDemo2.ViewModels
{
public class Order02VM
{
public List<ActiveOrder02> ActiveOrders02 { get; set; }
}
public class ActiveOrder02
{
public decimal ID { get; set; }
public string Client { get; set; }
}
}
EDIT: What I'm after is by DisplayNameFor to present the name of column tableClient from SampleDbTable as a Header of column Client in the Index view.
Thank you!
model.ActiveOrders02.Clientwon't work. It wouldn't work in C#, therefore it wouldn't work in Razor. AList<ActiveOrder02>doesn't have a property calledClient. TheClientproperty is on the items in the list, not the list itself. So it's not clear what you're trying to do. If theClientproperty makes more sense on theOrder02VM, then move it there. Or it it's defined in the correct place, then change how you're accessing it.model.ActiveOrders02.First().Clientfor example.