我将2个数据表传递到一个视图中,但是我收到了一个错误

时间:2015-12-29 05:15:30

标签: asp.net-mvc-4 asp.net-mvc-5.2

我收到了一个错误:

  

传递到字典中的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery`1 [jetjourneys.ViewModel.ItineraryPage]',但此字典需要类型为'jetjourneys.ViewModel.ItineraryPage <的模型项/ p>

代码:

public class ItineraryPage
{
    public PackageGallery Package { get; set; }
    public tblItinerary Itinerarydetail { get; set; }
}

public ActionResult ItineraryPackage(int id)
{
        using (travelAndTourismEntities objentity = new travelAndTourismEntities())
        {
            var query = (from x in objentity.tblItineraries
                         from y in objentity.PackageGalleries
                         where x.Imageid == id && y.Imageid == id
                         select new ItineraryPage { Package = y, Itinerarydetail = x });
            return View(query);
        }
}

这是我的控制员请帮助我,我犯了错误

1 个答案:

答案 0 :(得分:0)

您仍然返回IQueryable对象而不是实际结果,这应该可以正常工作

FirstOrDefault()

public ActionResult ItineraryPackage(int id)
{
        using (travelAndTourismEntities objentity = new travelAndTourismEntities())
        {
            var query = (from x in objentity.tblItineraries
                         from y in objentity.PackageGalleries
                         where x.Imageid == id && y.Imageid == id
                         select new ItineraryPage { Package = y, Itinerarydetail = x });

            return View(query.FirstOrDefault()); //Update here
        }
}