Reputation: 1541
I am developing a MVC application.
I want to display the list of Items of a perticuler object in View.
In below class List of InventoryItem is the part of the Product which can have n number of items.
I have following classes in model.
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public List<InventoryItem> Inventory { get; set; }
}
public class InventoryItem
{
public int Id { get; set; }
public Product Product { get; set; }
public Location Location { get; set; }
public int Quantity { get; set; }
}
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
}
Now I have created the one product with four Inventory Items...
public List<Product> GetAllProductList()
{
Product oProduct = new Product();
oProduct = oProdRepo.GetProductById(1);
oProduct.Inventory = new List<InventoryItem>
{
new InventoryItem { Location = oLocationRepo.GetLocationById(1),Quantity = 234,Product = oProduct},
new InventoryItem { Location = oLocationRepo.GetLocationById(2),Quantity = 123,Product = oProduct},
new InventoryItem { Location = oLocationRepo.GetLocationById(3),Quantity = 642,Product = oProduct},
new InventoryItem { Location = oLocationRepo.GetLocationById(4),Quantity = 534,Product = oProduct}
};
InventoryProductList.Add(oProduct);
return InventoryProductList;
}
Above method creates the Product with 4 Inventory Items. Its working perfectly.
Now I wan to display the above Inventory Items i.e 4 in the grid columns .
I am stuck on how to show the Grid columns for these InventoryItems ?
I have this view code....
@model IEnumerable<StockWatchServices.DomainClass.Product>
@Html.Grid(Model).Columns(columns =>
{
columns.Add(c => c.Name).Titled("Product Name").SetWidth(175).Sortable(true).Filterable(true);
1-> columns.Add(c => c.Inventory.Where(r=>r.Location.Nam ????? ) What to write here ?
2 Column-> ?
3 Column-> ?
4 Column-> ?
}).WithPaging(5)
Upvotes: 0
Views: 467
Reputation: 33384
I would first get all possible location names and then create a column for each one of them:
var locationNames = Model.SelectMany(n => n.Inventory).Select(n => n.Location.Name).Distinct().ToArray();
foreach(var locationName in locationNames)
{
var columnName = locationName;
columns.Add(m => m.Inventory.Single(n => n.Location.Name == columnName).Quantity, columnName).Titled(columnName);
}
EDIT
I've downloaded same Grid.MVC NuGet package and you need to use overload of Columns.Add
that accepts columnName
parameter otherwise it will complain about column duplicates
Caveat is that this will work assuming that all Product
will have same list of Location
names otherwise it will throw exception
Upvotes: 0
Reputation: 33384
Since, it seems, Grid
does not allow adding same property name more the once you can create table
manually:
<table>
<thead>
<tr>
@{
var locationNames = Model.SelectMany(n => n.Inventory).Select(n => n.Location.Name).Distinct().ToArray();
foreach (var locationName in locationNames)
{
<td>@locationName</td>
}
}
</tr>
</thead>
<tbody>
@foreach(var product in Model)
{
<tr>
@foreach(var columnName in locationNames)
{
<td>@product.Inventory.Single(i => i.Location.Name == columnName).Quantity</td>
}
</tr>
}
</tbody>
</table>
and apply some CSS to that
Upvotes: 1