I’m getting this error.
The model item passed into the dictionary is of type ‘System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType13[ACapture.Models.Blog,ACapture.Models.Image,ACapture.Models.Profile]]’, but this dictionary requires a model item of type ‘System.Collections.Generic.IEnumerable`1[ACapture.Models.Blog]’.
Here’s my controller.
public class HomeController : Controller
{
private ACaptureDB db = new ACaptureDB();
public ViewResult Index()
{
ViewBag.Message = "ArchiCapture";
var dateCheck = DateTime.Now.AddDays(-7);
var results = (from r in db.Blog
join p in db.Image on r.ImageID equals p.ImageID
//join l in db.Account on p.AccountID equals l.AccountID
join a in db.Profile on p.AccountID equals a.AccountID
//where r.dte_created >= dateCheck
select new { r, p, a });
return View(results);
}
Here’s my view.
@model IEnumerable<ACapture.Models.Blog>
@{
ViewBag.Title = "Home Page";
}
<div class="Forum">
<p>The Forum</p>
@foreach (var item in Model)
{
<div class="ForumChild">
<img src="@item.Image.img_path" alt="Not Found" />
<br />
<table>
@foreach (var comment in item.comment)
{
var db = new ACapture.Models.ACaptureDB();
var Name = from p in db.Profile
where (p.AccountID == comment.AccountID)
select new { p.FirstName, p.LastName };
<tr><td></td><td>@comment.Commentation</td></tr>
}
</table>
</div>
}
</div>
The error occurs whenever the data is passed from the controller to the view.
Also I need the data from all three tables Blog, Image, and Profile
Your View is expecting an IEnumerable of ACapture.Models.Blog. However, your query and join are return a new {Blog, Image, Profile}. Just like when you do a join in SQL and don’t specify the columns, you get them all back. This creates an anonymous type. You just want to select{r} for Blog