多对多关系的CRUD视图,复选框 [英] CRUD Views For Many-Many Relationship, Checkboxes

查看:88
本文介绍了多对多关系的CRUD视图,复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚我需要做些什么才能使它起作用.我正在用EF学习ASP.NET MVC CodeFirst.如果创建模型,则可以简单地为该模型添加一个控制器并添加脚手架以创建自动处理CRUD的视图.但是现在我有两个模型,Project和Category.它们具有多对多关系,并且数据库是使用关联表正确设计的,而无需为其创建单独的模型.模型的代码是这样的....

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?

推荐答案

有关在类似情况下使用复选框的示例,请参见本教程中的将课程分配添加到教师编辑页面中:

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

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆