Hi everyone I am building an application that allows the users to submit posts and I am trying to use orderby on a list in ascending order but I get this error
Error
The model item passed into the dictionary is of type 'System.Linq.OrderedEnumerable'2[myproject.Models.NewsPostVM,System.Nullable'1[System.Int32]]', but this dictionary requires a model item of type 'System.Collections.Generic.List'1[myproject.Models.NewsPostVM]'. Controller
public ActionResult News()
{
var posts = db.NewsPosts.Select(d => new NewsPostVM()
{
ID = d.ID,
Heading = d.Heading,
Body = d.Body,
Images = d.Images.Select(i => new NewsImageVM()
{
Path = i.Path,
FileName = i.DisplayName
}).ToList()
}).ToList();
var post= posts.OrderBy(m => m.ID);
return View(post);
}
view
@model List<MyProject.Models.NewsPostVM>
@foreach (var p in Model)
{
<div class="w3-container w3-center">
<h5><b>@p.Heading</b></h5>
<p>@p.Body</p>
@foreach (var image in p.Images)
{
<img class="img-thumbnail" width="150" height="150" src="~/Images/@image.Path" />
}
<h6>@p.Date</h6>
</div>
}