2

我想在编辑时的列表框中显示教师和教师的所有分配课程(多对多关系:课程、教师和 CourseTeacher)。(我想突出显示已经选择的课程,如果用户更改选择,它将反映在数据库中)。

我的问题是它适用于 Get 方法,但是当我发布更新教师和相关课程时,它不会自动执行(更新教师信息,但不是选定的课程),尽管它在对象中分配了所有课程。

我是 MVC 的新手,所以我什至不确定我的模型是否正确。但我想问题出在 post 方法中。(最后一段代码,请参阅我的评论)。

要清楚。我已经有可用的课程列表。我只想把他们和几位老师联系起来。

到目前为止我所做的:ViewModel

public class TeacherEditor
    {
        public Teacher Teacher { get; set; }
        public int[] SelectedCourseIds { get; set; }
        public MultiSelectList Courses { get; set; }
    }

教师模型:

 public class Teacher
    {
        public int TeacherID { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

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

            public Teacher()
          {
              Courses = new HashSet<Course>();
          }
    }   

看法:

 <div class="editor-field">
                     @Html.ListBoxFor(model=>model.SelectedCourseIds, Model.Courses)
        </div>

控制器获取:

 public ActionResult Edit(int id = 0)
        {
            var teacher = db.Teachers.Include(x=>x.Courses).Where(x=>x.TeacherID==id).SingleOrDefault();
            if (teacher==null)
            {
                return HttpNotFound();
            }
            var selectedCourses = teacher.Courses.Select(x=>x.CourseID);
            var model = new TeacherEditor
            {
                Teacher = teacher,
                Courses = new MultiSelectList(db.Courses, "CourseID", "Name", selectedCourses),
                SelectedCourseIds = selectedCourses.ToArray()
            };
            if (model == null)
            {
                return HttpNotFound();
            }
            return View(model);
        }     

控制器 POST:

 [HttpPost]
        public ActionResult Edit(TeacherEditor teacherEditor)
        {
            if (ModelState.IsValid)
            {
                    foreach (var id in teacherEditor.SelectedCourseIds)
                    {
                        Course course = db.Courses.Where(x=>x.CourseID==id).SingleOrDefault();
                        teacherEditor.Teacher.Courses.Add(course); //Here I'm adding courses back to Teacher object for save.
                    }
                db.Entry(teacherEditor.Teacher).State = EntityState.Modified; /?This doesn't save records to my relationship table. Why?
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(teacherEditor);
        }
4

1 回答 1

3

您的问题是您无法在 POST 上绑定到 MVC(ICollection)中的接口。DefaultModelBinder 不支持绑定到抽象基类和接口。更改ICollection<Course> Courses为具体的实现,例如List<Course> Courses.

然后,您可以在视图中使用索引符号表示课程,ListBoxFor 应该处理它。

尝试类似:

public class Teacher
    {
        public int TeacherID { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public IQueryable<Course> Courses { get; set; }

        public Teacher()
        {
            Courses = new IQueryable<Course>();
        }
    } 

public class TeacherEditor
    {
        public Teacher Teacher { get; set; }
        public int[] SelectedCourseIds { get; set; }
        public List<Course> CourseList 
        {
            get { return Teacher.Courses.ToList(); }
            set { Teacher.Courses = value.ToList(); }
        }
    }
于 2013-02-03T21:51:29.653 回答