传递到字典中的模型项是 Razor mvc5 中的

我收到以下错误:


传递给字典的模型项的类型为“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; }

}

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


慕姐4208626
浏览 158回答 1
1回答

至尊宝的传说

您选择匿名对象List<EventGalleryViewModel> list = db.EventGallerys.GroupBy(eg => new&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventId = eg.EventId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Title = eg.Title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventDate = eg.EventDate,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description = eg.Description,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThumbImage = eg.ThumbImage&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .OrderByDescending(eg => eg.Key.EventDate)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Take(5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .AsEnumerable()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(eg => new EventGalleryViewModel()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventId = eg.Key.EventId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Day = eg.Key.EventDate.ToString("D"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Month = eg.Key.EventDate.ToString("MMM"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Year = eg.Key.EventDate.ToString("yyyy"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Title = eg.Key.Title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description = eg.Key.Description,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ThumbImage = eg.Key.ThumbImage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).toList();&nbsp; &nbsp; &nbsp; &nbsp; return View(list);我希望这能解决你的问题
打开App,查看更多内容
随时随地看视频慕课网APP