mvc 5 中的异常,模型未在视图中传递数据
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);
}
传入字典的模型项的类型为'System.Collections.Generic.List1[<>f__AnonymousType3
3[System.Nullable1[System.Int32],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[PetsApplication.Models.Products]
错误消息不言自明!在您的 LINQ 表达式中,您将结果投影到匿名对象列表并将其传递给视图。但是您的视图被强类型化为 Products
class.
的集合
将您的匿名对象投影更改为使用 Product
class 的投影。
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);
您的视图是强类型的,并且您正在传递匿名类型创建一个 class 列表项,然后将其用作视图模型以将数据传递给视图。
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);
}
传入字典的模型项的类型为'System.Collections.Generic.List1[<>f__AnonymousType3
3[System.Nullable1[System.Int32],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[PetsApplication.Models.Products]
错误消息不言自明!在您的 LINQ 表达式中,您将结果投影到匿名对象列表并将其传递给视图。但是您的视图被强类型化为 Products
class.
将您的匿名对象投影更改为使用 Product
class 的投影。
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);
您的视图是强类型的,并且您正在传递匿名类型创建一个 class 列表项,然后将其用作视图模型以将数据传递给视图。