I fail to update a many-to-many relationship. When I debug "Response.Write(teacher.skills);", the values seem right but the database is not updating the object.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,lastname,firstname,image,campusId,skillIds")] Teacher teacher)
{
if (ModelState.IsValid)
{
if (teacher.skillIds != null)
{
teacher.skills = (from t in db.Skills.ToList() where teacher.skillIds.Contains(t.id) select t).ToList();
}
Response.Write(teacher.skills);
// 1st attempt -->
//db.Teachers.Attach(teacher);
//db.Entry(teacher).State = EntityState.Modified;
// 2nd attempt -->
UpdateModel(teacher);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(teacher);
}