I’m trying to use WebGrid HtmlHelper to display data from a few entities. The source for WebGrid is prepared by a linq query: var query = db.Orders.Include(o => o.OrderStatus).
I’m able to display aggregated data from two entities and it works fine.
But in another case when I’m trying to do the same but with another set of data I’ve got the problem I can’t solve.
My Controller query:
var query = db.SerNum.Include(o => o.Orders).Include(o => o.Item)
My View webgrid:
@grid.GetHtml(columns: grid.Columns(
grid.Column("Item.Name", "Item Name"),
grid.Column("Price", "Price"),
grid.Column("Order.shpOrderID", "Order ID"),
grid.Column("SellDate", "Date")
))
When I’m debugging my project I get an error.
WebGrid can’t be displayed – Item.Name and Order.shpOrderID do not exist
That is because not all SerNum have a linked record in the Order and Item tables. Can somebody advice me how to solve this?
I would suggest using a DTO, a POCO that is not being tracked and has not ties to EF. This DTO object will match the data you need for your grid and will be called something like
ProductGridView. This will make your queries much more efficent ad every.Include()you use in LINQ selects every column of data from that table. Here is a simple example, just keep in mind I don’t know what the properties on your entities looks like:You can now change your link query like so:
You can now return these results to your grid and it should work and will be much more efficient.
Here is some more info on DTO’s: http://rlacovara.blogspot.com/2009/03/what-is-difference-between-dto-and-poco.html