0

我有一个模型如下,想显示来自 4 个不同表的项目。

public class AuctionViewer
{
 public int AuctionId { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public DateTime  startTime { get; set; }
public DateTime  EndTime { get; set; }
public string Category { get; set; }
public int ExpectedHit { get; set; }
public string Status { get; set; }
}

下面是我的控制器代码。

 public ActionResult Index()
       {
      MightyMouseContainer ctx = new MightyMouseContainer();

       var  NewList = new AuctionViewer
             { 
                     from CT in ctx.Categories 
                     join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
                     join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID 
                     join ST in ctx.Status on AU.Status_ID equals ST.Status1
                     orderby AU.Auction_ID
                     select new
                     {
                       AuctionId = AU.Auction_ID, 
                       ProductName = PD.Item_name,
                       Price= PD.Item_Value,
                       startTime = AU.Start_Time,
                       EndTime = AU.End_Time,
                       Category = CT.Category_Description,
                       Status = ST.Description
                    };


    }

        return View(NewList);
    }

我想知道为什么会出现错误。请指教。我已经为此工作了很长一段时间,并意识到我需要一些帮助才能继续前进。我将感谢其他人的迅速反应以继续前进。谢谢

4

1 回答 1

0

假设您的视图接受 的模型IEnumerable<AuctionViewer>,您应该更改选择以返回强类型集合,例如:

var  NewList = from CT in ctx.Categories 
                     join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
                     join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID 
                     join ST in ctx.Status on AU.Status_ID equals ST.Status1
                     orderby AU.Auction_ID
                     select new AuctionViewer
                     {
                       AuctionId = AU.Auction_ID, 
                       ProductName = PD.Item_name,
                       Price= PD.Item_Value,
                       startTime = AU.Start_Time,
                       EndTime = AU.End_Time,
                       Category = CT.Category_Description,
                       Status = ST.Description
                    };

return View(NewList);

编辑从您的第二条错误消息来看,该视图似乎需要一个List<AuctionViewer>. 您可以更改控制器方法以实现 IQueryable,List如下所示:

var  NewList = (from CT in ctx.Categories 
                     join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
                     join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID 
                     join ST in ctx.Status on AU.Status_ID equals ST.Status1
                     orderby AU.Auction_ID
                     select new AuctionViewer
                     {
                       AuctionId = AU.Auction_ID, 
                       ProductName = PD.Item_name,
                       Price= PD.Item_Value,
                       startTime = AU.Start_Time,
                       EndTime = AU.End_Time,
                       Category = CT.Category_Description,
                       Status = ST.Description
                    }).ToList();

return View(NewList);
于 2012-09-17T12:54:23.670 回答