Checkboxes in CRUD Views for Many-Many Relationships checkbox code-first entity-framework many-to-many


I am having a hard time trying to figure out what I need to do to get this to work. I'm learning ASP.NET MVC CodeFirst with EF. If I make a model I can simply add a controller for that model and add scaffolding to create views that automatically take care of CRUD. But now I have two models, Project and Category. They have a many to many relationship and database is designed correctly with the associative table without having to make a separate model for it. The code for the models is this....

public class Project
    public int ProjectId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Testimonial { get; set; }

    public virtual ICollection<Image> Images { get; set; }
    public virtual ICollection<Category> Categories { get; set; }

    public Project()
        Categories = new HashSet<Category>();

public class Category
    public int CategoryId { get; set; }
    public string Name { get; set; }

    public ICollection<Project> Projects { get; set; }

    public Category()
        Projects = new HashSet<Project>();

So I add my controllers and do the scaffolding. I go in and create my categories just fine. But when it comes to my Projects/Create view, I would like to make it so that all the categories are displayed as checkboxes. Also, I would like to ensure that at least one category is selected before being able to submit the form to create a project. How would I do this?

2/5/2012 7:47:52 PM

Accepted Answer

For an example of using check boxes in a similar scenario, see Adding Course Assignments to the Instructor Edit Page in this tutorial:

2/6/2012 4:02:56 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow