[英]exception in mvc 5, model is not passing data in view
public ActionResult CustomerOrders()
{
string cookieName = FormsAuthentication.FormsCookieName; //Find cookie name
HttpCookie authCookie = HttpContext.Request.Cookies[cookieName]; //Get the cookie by it's name
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); //Decrypt it
string UserName = ticket.Name;
int UserID = context.Registers.Where(x => x.name == UserName).Select(x => x.id).FirstOrDefault();
var data = (from i in context.Registers.Where(x => x.id == UserID)
join
c in context.Orders on i.id equals c.customer_id
into egroup
from k in egroup
join p in context.Products
on k.product_id equals p.product_id
select new
{
p.price,
// k.order_total,
p.ImageUrl,
p.ProductName
}).ToList() ;
Products pr = new Products();
return View(data);
// return View(data);
}
The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[<>f__AnonymousType3
3[System.Nullable 1[System.Int32],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[PetsApplication.Models.Products] 传递到字典中的模型项的类型为'System.Collections.Generic.List
1[<>f__AnonymousType3
3 [System.Nullable 1[System.Int32],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [PetsApplication.Models.Products] 1[System.Int32],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
The error message is self explanatory! 错误消息不言自明! In your LINQ expression, you are projecting the result to a list of annonymous objects and passing that to the view.
在LINQ表达式中,您将结果投影到一个匿名对象列表中,并将其传递给视图。 But your view is strongly typed to a collection of
Products
class. 但是,您的视图强烈属于
Products
类的集合。
Change your anonymous object projection to the projection using Product
class. 使用
Product
类将匿名对象投影更改为投影。
var data = (from i in context.Registers.Where(x => x.id == UserID)
join c in context.Orders on i.id equals c.customer_id
into egroup
from k in egroup
join p in context.Products
on k.product_id equals p.product_id
select new Products
{
price=p.price,
ImageUrl=p.ImageUrl,
ProductName=p.ProductName
}).ToList();
return View(data);
视图是强类型的,并且要传递匿名类型,以创建一个列表项类,然后将其用作视图模型以将数据传递给视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.