【问题标题】:How to Insert checkbox checked values id in Many to Many relationship table?如何在多对多关系表中插入复选框选中的值 id?
【发布时间】:2019-07-23 19:42:41
【问题描述】:

我想将复选框 id 的值添加到两个表 Course-Student 之间的多对多关系表中

我有 3 个表 Course{ CourseID,CourseName }, Studdent{StudentID,StudentName} 和 CourseStudet{CourseID, StudentID} 课程表已经有课程{1.DataScience,2.English,3.Physics} 在插入学生时,我已经显示了带有这些选项的课程复选框,从数据库中获取这些课程的列表以在用户即将注册时显示给用户,现在我很困惑如何插入数据库?

public class CourseModel {

        public List<Course> mycourse{ get; set; }

        public string StudentName {get; set;}


    }

EF generated Class

 public partial class Course
    {
        public Course()
        {
            this.Student= new HashSet<Student>();
        }

        public int CourseID { get; set; }
        public string CourseName { get; set; }

        i have inserted this field to check for checked value
        public bool Ischecked { get; set; }

        public virtual ICollection<Student> Student { get; set; }
    }


 public partial class Student
    {

        public Student()
        {
            this.Course= new HashSet<Course>();
        }

        public int StudentID { get; set; }
        public string StudentName { get; set; }


        public virtual ICollection<Course> Course{ get; set; }
    }

 public ActionResult Index()
        {
            CourseModel coursemodel = new CourseModel();
            using (Entities db = new Entities())
            {
                coursemodel.mycourse = db.Course.ToList<Course>();

                 return View(coursemodel);

            }


        }
        [HttpPost]
        public ActionResult Index(CourseModel course)
        {       
            return View(course);
        }

View

@using (Html.BeginForm("index", "Course", FormMethod.Post))

         <input type="Text" name="StudentName" placeholder="Name" />

{
    <table>
        @for (int i = 0 ; i < Model.mycourse.Count; i++)
        {
            if (i % 3 == 0) {
                @:<tr></tr>
            }
        <div>

            @Html.CheckBoxFor(x => x.mycourse[i].Ischecked)
            <label>@Model.mycourse[i].CourseName</label>
            @Html.HiddenFor(x => x.mycourse[i].CourseID)
            @Html.HiddenFor(x => x.mycourse[i].CourseName)
        </div>



        }

    </table>
   <input type="submit" value="Submit" />
}

我正在获取复选框 id、名称和现在检查哪个课程我如何将包含这些值的学生添加到关系表 CourseStudent 中?

【问题讨论】:

  • 在 CourseStudent 中,您可以让一个学生学习许多课程。如果您显示插入学生的位置会很有帮助,这样我们就不会只是编写随机代码
  • 您可以为列表 I 添加范围。您的 CourseStudent 表或使用 for 循环插入每个。只需显示为学生插入的代码,我们将能够提供适当的帮助
  • 我刚刚在 CourseModel 中添加了 studentName 的属性,现在我将在 Controller 中获取参数,我将如何将学生姓名添加到学生表?并列出 ischecked == true 的 Checkbox 值我如何将它们的值添加到 CourseStudent 表?针对我们刚刚添加的学生?
  • 学生只有一个属性,这就是它的名字,为什么我要以不同的方式添加它?我不能在同一个控制器中添加两者吗?
  • 你检查新答案了吗?

标签: asp.net-mvc


【解决方案1】:

您的数据库设计有问题。删除 ischecked 字段。请记住,在设计实体时,不应放置用于查看的字段。就像我从每个查询中制作报告表一样。

修正你的模型

  public partial class Course
{
    public Course()
    {
        this.Student = new HashSet<Student>();
    }

    public int CourseID { get; set; }
    public string CourseName { get; set; }

    public virtual ICollection<Student> Student { get; set; }
}


public partial class Student
{

    public Student()
    {
        this.Course = new HashSet<Course>();
    }

    public int StudentID { get; set; }
    public string StudentName { get; set; }

    public virtual ICollection<Course> Course { get; set; }
}

添加视图模型(您可以使用视图模型为您的视图显示自定义数据) You can read more

视图模型

public class CourseViewModel
{
    public Course course { get; set; }
    public bool CheckBox { get; set; }
}

public class StudentCourseViewModel
{
    public List<CourseViewModel> coursesVM { get; set; }
    public Student student { get; set; }
}

控制器

  public ActionResult index()
  {
      Entities db = new Entities();
      List<CourseViewModel> courselist = new List<CourseViewModel>();
      foreach (var item in db.Course.ToList())
        {
            courselist.Add(new CourseViewModel { course = item});
        }
      StudentCourceViewModel model = new StudentCourseViewModel(
      {
          student = new student(),
          coursesVM = courselist   }
      };
      return View(model);
  }
  [HttpPost]
  public ActionResult Save(StudentCourseViewModel model)
  {
      Entities db = new Entities();
      Student stdindb = db.Students.FirstorDefault(c => c.StudentName == model.student.StudentName); 
      if(stdindb == null)
      {
       stdindb = new student(){//your properties};
       db.students.add(stdindb);
      }
      foreach (var item in model.coursesVM)
      {
        if (item.Ischecked)
          {
            stdindb.Course.Add(db.course.single(c=>c.CourseId == item.course.CourseId ));
          }
        }
        db.SaveChanges();
        return View();
  }

查看

@model SchoolCMS.Models.StudentCourseViewModel
@{
    ViewBag.Title = "Select Course";
}

@using (Html.BeginForm("Save", "home", FormMethod.Post))
{
@Html.TextBoxFor(c=>c.student.StudentName)
<table>
    @for (int i = 0; i < Model.coursesVM.Count; i++)
    {
        <tr>
            <td>
            @Html.CheckBoxFor(c => c.coursesVM[i].Ischecked)
            @Html.HiddenFor(c => c.coursesVM[i].course.CourseID)
        </td>
        <td>@Html.DisplayFor(c => c.coursesVM[i].course.CourseName)</td>
    </tr>
    }
    </table>
    <input type="submit" value="Submit" />
}

【讨论】:

  • coursesVM = new CourseViewModel {course = db.Course.ToList() 此行错误“无法将类型 system.collections.generic.list 隐式转换为 Course”
  • @SohailShah 查看新答案
  • 谢谢你的时间,但仍然是这一行“courselist.Add(new CourseViewModel { course = item});”在项目上给出相同的错误
  • @SohailShah 欢迎您,它们的类型相同。什么错误?我没有来源,你可以猜出什么是错的。检查类型
  • 好吧,这仅适用于我已经通过其他方法显示列表的视图,我的原始问题仍然存在,我正在获取复选框 ID、名称以及现在检查哪个课程我如何添加学生包括这些值到关系表 CourseStudent ?发布后你能帮我吗?
猜你喜欢
  • 2013-12-09
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 2015-11-09
  • 1970-01-01
相关资源
最近更新 更多