I'm really new at all of this. I'm currently reading the tutorial "Getting Started with EF using MVC" on the ASP.NET website: (Chapter 6)
In the tutorial, at the part titled "Adding Course Assignments to the Instructor Edit Page" The author wrote about how to edit the Course in the instructor page:
public ActionResult Edit(int id)
{
Instructor instructor = db.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.Courses)
.Where(i => i.InstructorID == id)
.Single();
PopulateAssignedCourseData(instructor);
return View(instructor);
}
public ActionResult Edit(int id, FormCollection formCollection, string[] selectedCourses)
{
var instructorToUpdate = db.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.Courses)
.Where(i => i.InstructorID == id)
.Single();
if (TryUpdateModel(instructorToUpdate, "", null, new string[] { "Courses" }))
{
try
{
if (String.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location))
{
instructorToUpdate.OfficeAssignment = null;
}
UpdateInstructorCourses(selectedCourses, instructorToUpdate);
db.Entry(instructorToUpdate).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch (DataException)
{
//Log the error (add a variable name after DataException)
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}
}
PopulateAssignedCourseData(instructorToUpdate);
return View(instructorToUpdate);
}
Could some one please tell me how to use the same concept (as the author) to accomplish deleting and creating action method? Or if you can direct me to a useful tutorial/site about many to many relationship with EF in MVC, especially on how to create a controller and view for many to many related data, similar to the tutorial mentioned above. I'm a beginner at this, but I still need to get work done, so it would really help if the concepts used coincides, it would be easier for me.
Thanks so much in advance!