我正在使用 Microsoft VS 2010 C#、MVC3。我有具有多对多关系的 Calsserooms 和 Student,因此我添加了一个名为 Classroom_Students 的中间表。将学生添加到教室时,我在视图中使用了一个组合框,其中填充了所有学生的姓名,我在每次提交时一一选择
@using (Html.BeginForm("AddStudentToClassroom", "Calssrooms", FormMethod.Post))
{
@Html.LabelFor(c=>c.Students, "Choose a Student")
<select name = "StudentID">
@foreach (var it in Model.Students)
{
<option value="@it.ID">@it.StudentName </option>
}
</select>
<input type="submit" value= "Add" />
}
我的问题是:如何使用 gride 而不是这个组合来选择许多学生,全选或取消全选以添加?我会很感激任何帮助。
这是我的控制器中的代码。对于页面第一次调用,我填写组合框如下:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddStudentToClassroom(int id) //id of calssroom
{
using (ExaminationEntities en = new ExaminationEntities())
{
ClassroomDetails ClsromView = new ClassroomDetails (); // these are for
ClsromView.Classroom = en.Classroom.Single(c => c.ID == id);// loading calssroom information and to
ClsromView.Students = en.Students.ToList(); // fill students list for the combobox
return View(ClsromView);
}
}
在提交表单、视图、点击添加按钮时,会调用如下重载的添加函数来保存数据:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddStudentToClassroom(AddStudToCals ClasStud) //ClasStud is the submited data from the view
{
using (ExaminationEntities en = new ExaminationEntities())
{
ClassroomDetails ClsromView = new ClassroomDetails(); // these are for
ClsromView.Calssroom = en.Calssroom.Single(c => c.ID == ClasStud.ClassroomID); // loading calssroom information and to
ClsromView.Students = en.Student.ToList(); // fill students list for the combobox
using (ExaminationEntities exn = new ExaminationEntities())
{
Calssroom_Student To_DB_ClasStud = new Calssroom_Student (); //To_DB_ClasStud object to get the submited values and to save it in the DB
To_DB_ClasStud.CalssroomID = ClasStud.CalssroomID;
To_DB_ClasStud.StudentID = ClasStud.StdentID;
en.AddToClassroom_Student(To_DB_ClasStud);
en.SaveChanges();
}
return View(ClsromView);
}
}