I am trying to access the data on the many side of a one to many relationship. In the Index action of my controller I assign the data to a variable and return it. I have a Blog, the one side, with many posts. I just want to list the blogs with their posts in the view. For example...
Blog1
post1
post2
post3
Blog2
post1
post2
That s pretty much it. But as it stands, in the view, I am unable to access the post data.
Here are my models with the relevant properties
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post>? Posts { get; set; } = new HashSet<Post>();
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public int BlogId { get; set; }
And here is the controller
public async Task<IActionResult> Index()
{
var applicationDbContext = _context.Blogs
.Include(b => b.BlogUsers)
.Include(p => p.Posts)
.ToListAsync();
return View(await applicationDbContext);
}
I ve included the Posts so I should be able to access the Post data. I can access the Blogs data no problem.
Any ideas?