I would like to retrieve several items in one cell of a table in my view. I have several models, Product, Pack(as category), Order and OrderDetail. I think to achieve what I want, I should create a view model with data from my Models, but I don’t really know how to do this and wich data i should bind.
This my view
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.OrderId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClientID)
</td>
<td>
@Html.DisplayFor(modelItem => item.SiteNumber)
</td>
<td>
@if (item.Pack == null)
{
<ul>
@foreach (var tmp in item.????) //PRODUCT
{
<li>Html.DisplayFor(modelItem => tmp.Name) </li>
}
</ul>
}
else
{
<ul>
@foreach (var tmp in item.????) //PACK
{
<li>Html.DisplayFor(modelItem => tmp.Name) </li>
}
</ul>
}
</td>
<td>
@{
if (item.Pack == null)
{
<ul>
@foreach (var tmp in item.????) // PRODUCT
{
<li>Html.DisplayFor(modelItem => tmp.UnitPrice)
</li>
}
</ul>
}
else
{
<ul>
@foreach (var tmp in item.????) //PACK
{
<li>
@Html.DisplayFor(modelItem => tmp.UnitPricePack)
</li>
}
</ul>
}
}
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Total)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderDate)
</td>
</tr>
I would like my table looks like that at the end :

My question again, How can I create a ViewModel to retrieves data in my view and for it match with my @foreach condition inside ma table… I don’t know if I am clear enough… This post follow this one . Actually, what i’ve done returns me error on error, I don’t really know how to set up my viewmodel, which data I need to bind and how. Thanks for your help, your advices, I take all
You don’t necessarily need a new ViewModel, Order has all you need. Your model will be a list of orders.
Update according to your update:
You are still trying to pass collection of orderdetails instead of orders
instead of this:
Have this ( assuming you have Orders table):