在我的MVC 3应用程序我使用下面的查询将值返回到View:LINQ/EF - 使用的GroupBy
var items = context.QuoteLines.Include("DaybookVehicles").Where(x => x.EnquiryID == id).GroupBy(x=>x.VehicleID).ToList();
return View(items);
然后在我看来,我有这样的顶部:
@model IEnumerable<myApp.Areas.DayBook.Models.DayBookQuoteLines>
但是,这将引发一个错误:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[System.Linq.IGrouping`2[System.Int32,myApp.Areas.DayBook
.Models.DayBookQuoteLines]]', but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[myapp.Areas.DayBook.Models.DayBookQuoteLines]'.
如何在这种情况下,使用一组由?当我想组我的每一个QuoteLines
其匹配VehicleID
编辑
QuoteLines型号:
public class DayBookQuoteLines
{
[Key]
public int QuoteLineID { get; set; }
public int EnquiryID { get; set; }
public virtual int VehicleID { get; set; }
public virtual DaybookVehicles Vehicle { get; set; }
[Required(ErrorMessage = "This field is required.")]
[Display(Name = "Item Number:")]
[MaxLength(50)]
public string ItemNumber { get; set; }
public string ItemDescription { get; set; }
}
车辆型号:
public class DaybookVehicles
{
[Key]
public int VehicleID { get; set; }
public int EnquiryID { get; set; }
public virtual ICollection<DayBookQuoteLines> QuoteLines { get; set; }
public string vrm { get; set; }
public string make { get; set; }
public string model { get; set; }
public string engine_size { get; set; }
public string fuel { get; set; }
}
我觉得我有这个设置正确,但每辆车都可以有一系列QuoteLines附加到它!
您使用它首先阅读文档,然后阅读错误代码,然后解释你想要什么,因为查询和@model说完全不同。 – Euphoric
我们需要您的模型来帮助您! –
查询返回(许多)*组*,其中每一个都有一个'VehicleID'和许多'QuoteLines'。您的视图试图简单显示'QuoteLines'。这些是不同类型的数据。决定是否希望您的视图显示* groups *或* lines *,并适当修改您的查询。 – AakashM