CRUD-views on many-many relationships, flags

It's hard for me to try to figure out what I need to do to get this to work. I am learning ASP.NET MVC CodeFirst with EF. If I create a model, I can simply add a controller for this model and add scaffolding to create views that automatically take care of CRUD. But now I have two models: Project and Category. They have many different relationships, and the database is correctly designed using an associative table without creating a separate model for it. Code for models is ....

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 make scaffolding. I enter and create my categories very well. But when it comes to my project / creation, I would like to make all categories appear as checkboxes. In addition, I would like to make sure that at least one category is selected before you can submit a form to create a project. How should I do it?

+4
source share
1 answer

For an example of using flags in a similar scenario, see the section Adding Course Assignments to the Instructor's Edit Page :

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc- application

+9
source

Source: https://habr.com/ru/post/1394897/


All Articles