I have this LINQ…
var n = from a in this.DataContext.News select new { a.ID, a.Name, a.StartDate, a.EndDate, SportName = (from v in this.DataContext.Sports where v.ID == a.SportID select v).FirstOrDefault()};
ViewData["News"] = n.ToList();
And I need to generate a HTML table. I cannot figured out how to get items from ViewData[“News”]. Any clue?
<% foreach (var item in (???) ViewData["News"])
{ %> ...
UPDATES:
I found this post It seems I have to create 1 extra class NewsView which is going to have all selected fields. And then I can cast to NewsView… Is there other way to do it?
var n= from a in this.DataContext.News
select new NewsView { ID = a.ID, Name = a.Name, StartDate = a.StartDate, EndDate = a.EndDate,
SportName = (from v in this.DataContext.Sports where v.ID == a.SportID select v).FirstOrDefault().Name};
and
<% foreach (var item in (IEnumerable<MyProject.ViewModels.NewsView>)ViewData["News"])
{ %>
The best thing to do will be to create a class in your model so that you have a List items to assign your data to and then to repeat through rather than using ViewData or Viewbags. Then in your foreach loop, the var item will have dot (.) properties.