Razor mvc5中传递给字典的模型项的类型为'System.Collections.Generic.List。

时间:2018-08-04 11:05:17

标签: c# asp.net linq razor asp.net-mvc-5

我遇到以下错误:

  

传递到字典中的模型项的类型为'System.Collections.Generic.List

下面是我的操作方法并返回正确的结果

   public ActionResult Gellery()
    {

        var list = db.EventGallerys.GroupBy(eg => new
        {
            EventId = eg.EventId,
            Title = eg.Title,
            EventDate = eg.EventDate,
            Description = eg.Description,
            ThumbImage = eg.ThumbImage
        })
                              .OrderByDescending(eg => eg.Key.EventDate)
                              .Take(5)
                              .AsEnumerable()
                              .Select(eg => new
                              {
                                  EventId = eg.Key.EventId,
                                  Day = eg.Key.EventDate.ToString("D"),
                                  Month = eg.Key.EventDate.ToString("MMM"),
                                  Year = eg.Key.EventDate.ToString("yyyy"),
                                  Title = eg.Key.Title,
                                  Description = eg.Key.Description,
                                  ThumbImage = eg.Key.ThumbImage
                              }).ToList();

      //  var list = db.EventGallerys.ToList();

        return View(list);
   }

我不确定我的视图部分是否正确

   @model List<IBAC.Models.EventGalleryViewModel>

  @{ ViewBag.Title = "Gellery"; }

 <h2>Gellery</h2>


 @foreach (var item in Model)
 {
 <a href='@Url.Content("~/GalleryImages/" + item.ThumbImage)'>
    <img class='thumbnail' src='@Url.Content("~/GalleryImages/" + item.ThumbImage)' />
   </a>

 }

下面是我的Viewmodelclass

 public class EventGalleryViewModel
{
    public string EventId { get; set; }
    public string Day { get; set; }
    public string Month { get; set; }
    public string Year { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string ThumbImage { get; set; }
}

我知道错误来自View html,请帮助我解决这个问题。谢谢您的努力。

1 个答案:

答案 0 :(得分:0)

您选择匿名对象

List<EventGalleryViewModel> list = db.EventGallerys.GroupBy(eg => new
        {
            EventId = eg.EventId,
            Title = eg.Title,
            EventDate = eg.EventDate,
            Description = eg.Description,
            ThumbImage = eg.ThumbImage
        })
                          .OrderByDescending(eg => eg.Key.EventDate)
                          .Take(5)
                          .AsEnumerable()
                          .Select(eg => new EventGalleryViewModel()
                          {
                              EventId = eg.Key.EventId,
                              Day = eg.Key.EventDate.ToString("D"),
                              Month = eg.Key.EventDate.ToString("MMM"),
                              Year = eg.Key.EventDate.ToString("yyyy"),
                              Title = eg.Key.Title,
                              Description = eg.Key.Description,
                              ThumbImage = eg.Key.ThumbImage
                          }).toList();
        return View(list);

我希望这能解决您的问题