多对多关系、复选框的 CRUD 视图

发布于 2025-01-03 11:14:36 字数 980 浏览 3 评论 0 原文

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

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>();
    }
}

所以我添加我的控制器并做脚手架。我进去创建我的类别就好了。但是当涉及到我的“项目/创建”视图时,我希望将所有类别显示为复选框。另外,我想确保在提交表单以创建项目之前至少选择一个类别。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-01-10 11:14:36

有关在类似场景中使用复选框的示例,请参阅本教程中的将课程作业添加到教师编辑页面

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-lated-data-with-the-entity-framework-in-an- asp-net-mvc-应用程序

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文