Such classes can be called Dto, ViewModel - and put them in separate from the folder with the models (if you have a single application). Do something like this:
Create a view model:
public class BookListViewModel { public int Val { get; set; } public DateTime Dat { get; set; } }
Make an action controller type:
public ActionResult Index() { var model = db.tbl .GroupBy(x => DbFunctions.TruncateTime(x.q_Date)) .Select (x => new { V = x.Count(), D = (DateTime)x.Key }) .AsNoTracking() .AsEnumerable() .Select (x => new BookListViewModel { Val = xV, Dat = (DateTime)xD }) .ToArray(); return View(model); }
(Here you need to understand the difference between IEnumerable and IQueryable, for example, see here )
You can also make a constructor in the class:
public class BookListViewModel { public BookListViewModel (int val, DateTime dat) { this.Val = val; this.Dat = dat; } public int Val { get; private set; } public DateTime Dat { get; private set; } }
And cut the record to:
public ActionResult Index() { var model = db.tbl .GroupBy(x => DbFunctions.TruncateTime(x.q_Date)) .Select (x => new { V = x.Count(), D = (DateTime)x.Key }) .AsNoTracking() .AsEnumerable() .Select (x => new BookListViewModel(x)) .ToArray(); return View(model); }
Or even go to the use avtomapera.
In the view, iterate the model:
@model IEnumerable<BookListViewModel> @foreach (var item in Model) { <tr> <td> @item.Dat </td> <td> @item.Val </td> </tr> }