LINQ Anonymous Types + MVC Views

后端 未结 6 1498
心在旅途
心在旅途 2021-01-03 00:40

I\'ve seen many questions about this, but i\'ve never really got the answer that I need.

I\'m converting a fairly large web application from Web Forms to MVC and af

6条回答
  •  悲哀的现实
    2021-01-03 00:56

    Rather than an anonymous type, create a type to hold the name and date:

    public class NameDate
    {
      public string Name { get; set; }
      public DateTime Date { get; set; }
    }
    

    Then use that in your Linq query:

    from p in db.Products select new NameDate { Name = p.Name, Date = p.Date }
    

    Strongly type your view to be MyView> and then just do a foreach ( var nameDate in ViewData.Model )...

提交回复
热议问题