I’ve seen this question asked on here, but it seems to be different than my situation. I may be wrong about that but we will see.
Right now I am creating a blog type website in MVC3 (C#) and I can currently create, edit, delete etc a blog just fine and everything works. I am using Code First EF, so I don’t know how much that matters.
I have a BlogPost model as follows:
public class BlogPost
{
public int id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public ICollection<Topic> Topics { get; set; }
public string Content { get; set; }
public ICollection<Comment> Comments { get; set; }
}
and a Topic model (each blog post can have multiple topics)
public class Topic
{
public int id { get; set; }
public string Name { get; set; }
public int PostId { get; set; }
// navigation back to parent
public BlogPost Post { get; set; }
}
Then there is my DbContext inherited model with all of my models in it:
public class MyModel : DbContext
{
public DbSet<BlogPost> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Topic> Topics { get; set; }
public DbSet<AdminComment> AdminComments { get; set; }
public DbSet<Bug> Bugs { get; set; }
}
Currently the BlogController is using the default scaffolding for create/edit/delete/details
private MyModel db = new MyModel();
//
// GET: /Admin/Blog/
public ViewResult Index()
{
return View(db.Posts.ToList());
}
What can I do to pass in the other model as well so say on this list, it will show all topics associated with the posts, and also add in a create for adding topics to a post you are currently creating?
Create an outer object that has the properties you want your view to be able to see, and use the new object as your model. You have pretty much already done this. Just change your controller to this:
Now the view has access to everything.