Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My actions

public ActionResult Orders()
{
NorthwindEntities db = new NorthwindEntities();
return View(db.Orders.ToList());


}

public ActionResult OrderDetails(int id)
{
NorthwindEntities db = new NorthwindEntities();


var query = from od in db.Order_Details
join p in db.Products
on od.ProductID equals p.ProductID
where od.OrderID == id
select new
{
od.OrderID,
od.ProductID,
p.ProductName,
od.Quantity,
od.UnitPrice,
od.Discount,

};

return View(query.ToList());

}

My view

@model List MvcApplication2.Models.Order_Detail
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<title>OrderDetails</title>
</head>
<body>

@{
foreach (var item in Model)
{
@Html.DisplayFor(x => item.OrderID)
@Html.DisplayFor(y => item.ProductID)
@Html.DisplayFor(y => item.Product.ProductName);
@Html.DisplayFor(z => item.Quantity)
@Html.DisplayFor(z => item.UnitPrice)
@Html.DisplayFor(z => item.Discount)
}
}

</body>
</html>

and my error

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType0`5[System.Int32,System.Int32,System.Int16,System.Decimal,System.Single]]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[MvcApplication2.Models.Order_Detail]'.
Posted

You can't pass a collection of anonymous types when the View expects a List of Model class .

See the @model declaration in the View and the select new {} part in your code.

Change the code to return List<order_details>

(or a ViewModel in case of join ,But then the @model declaration in View should change accordingly )

Try it.

Hope this helps...
 
Share this answer
 
You should useIEnumerable<table_name> instead of var because var is a ananymous type.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900