【发布时间】: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