ASP.NET MVC CORE - 多对多关系 - 创建视图
创始人
2024-09-16 11:01:14
0

要实现ASP.NET MVC Core的多对多关系和创建视图,可以按照以下步骤进行:

  1. 创建模型类和数据库上下文类:

首先,创建两个模型类,表示多对多关系的两个实体。例如,我们创建一个Student(学生)和一个Course(课程)模型类:

public class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public ICollection StudentCourses { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string Title { get; set; }
    public ICollection StudentCourses { get; set; }
}

public class StudentCourse
{
    public int StudentId { get; set; }
    public Student Student { get; set; }
    public int CourseId { get; set; }
    public Course Course { get; set; }
}

然后,创建一个继承自DbContext的数据库上下文类,并在其中设置多对多关系的联接表:

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet Students { get; set; }
    public DbSet Courses { get; set; }
    public DbSet StudentCourses { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasKey(sc => new { sc.StudentId, sc.CourseId });

        modelBuilder.Entity()
            .HasOne(sc => sc.Student)
            .WithMany(s => s.StudentCourses)
            .HasForeignKey(sc => sc.StudentId);

        modelBuilder.Entity()
            .HasOne(sc => sc.Course)
            .WithMany(c => c.StudentCourses)
            .HasForeignKey(sc => sc.CourseId);
    }
}
  1. 创建控制器和视图:

然后,创建一个控制器来处理学生和课程之间的多对多关系。例如,我们创建一个名为StudentController的控制器,并实现创建视图的动作方法:

public class StudentController : Controller
{
    private readonly SchoolContext _context;

    public StudentController(SchoolContext context)
    {
        _context = context;
    }

    // GET: /Student/Create
    public IActionResult Create()
    {
        ViewBag.Courses = _context.Courses.ToList();
        return View();
    }

    // POST: /Student/Create
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task Create(Student student, List selectedCourses)
    {
        if (ModelState.IsValid)
        {
            _context.Add(student);
            await _context.SaveChangesAsync();

            if (selectedCourses != null)
            {
                foreach (var courseId in selectedCourses)
                {
                    var studentCourse = new StudentCourse { StudentId = student.StudentId, CourseId = courseId };
                    _context.Add(studentCourse);
                }

                await _context.SaveChangesAsync();
            }

            return RedirectToAction(nameof(Index));
        }

        ViewBag.Courses = _context.Courses.ToList();
        return View(student);
    }
}
  1. 创建视图模板:

最后,创建一个包含表单和复选框列表的视图模板。例如,我们创建一个名为Create.cshtml的视图模板:

@model Student

@foreach (var course in ViewBag.Courses) {
@course.Title
}

以上是一个基本的示例,展示了如何在ASP.NET MVC Core中实现多对多关系和创建视图。根据你的具体需求,可能需要进行一些修改和调整。

相关内容

热门资讯

记者发布!pokemmo修改器... 记者发布!pokemmo修改器手机版,wepoker私人辅助器,总是真的是有挂(有挂助手)1、操作简...
实测分享!德普之星透视辅助软件... 实测分享!德普之星透视辅助软件是真的吗,德州hhpoker脚本,真是是有挂(有挂分享)1、下载好脚本...
揭秘真相!wepoker辅助器... 揭秘真相!wepoker辅助器官方,we poker辅助器,本来真的有挂(有挂总结)1、wepoke...
科技通报!wepoker怎么增... 科技通报!wepoker怎么增加运气,HH平台挂,一贯真的有挂(有挂细节)亲,关键说明,透视脚本安卓...
技术分享!hhpoker有没有... 技术分享!hhpoker有没有辅助,hhpoker脚本,真是真的有挂(有挂方式)1、很好的工具软件,...
一分钟揭秘!pokemmo手机... 一分钟揭秘!pokemmo手机版修改器,大菠萝辅助器,原来真的是有挂(有挂总结)1、让任何用户在无需...
9分钟了解!wepoker透视... 9分钟了解!wepoker透视苹果系统,wepoker辅助透视软件,果然存在有挂(有挂教学)1、we...
免费测试版!werplan辅助... 免费测试版!werplan辅助软件,德普之星透视辅助软件,确实是有挂(真的有挂)辅助器是一种具有地方...
玩家实测!wepoker有辅助... 玩家实测!wepoker有辅助插件吗,wepoker透视脚本是什么,切实真的是有挂(有挂总结)1、打...
科普分享!wpk可以作弊吗,h... 科普分享!wpk可以作弊吗,hhpoker德州有挂吗,原来是有挂(有人有挂)1、进入到是否有挂之后,...