--------------- Edit view get ----------- @model WebCourse.Models.EditEmployeeVm @{ Layout = null; } Edit
@using (Html.BeginForm()) {
Name: @Html.TextBoxFor(a => a.Name)
@Html.HiddenFor(g => g.Id) @Html.LabelFor(f => f.Name) @Html.DropDownList("AvailableCourses", Model.Courses, "Select")

Existing courses

foreach (var c in Model.ExistingCourses) {
@c.Name Remove
} }
------------------ custom view model --------------------- using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebCourse.Models { public class EditEmployeeVm { public int Id { set; get; } public string Name { get; set; } public List Courses { get; set; } public int[] CourseIds { set; get; } public List ExistingCourses { set; get; } } public class CourseVm { public int Id { set; get; } public string Name { set; get; } } } ---------------------- coursedata CONTROLLER --------------------- using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using WebCourse.Models; using System.Data.Entity; namespace WebCourse.Controllers { public class coursedataController : Controller { mycourseEntities db = new mycourseEntities(); // GET: coursedata public ActionResult Index() { return View(); } public ActionResult Edit(int id) { var vm = new EditEmployeeVm { Id = id }; var emp = db.Employees.FirstOrDefault(f => f.Id == id); vm.Name = emp.Name; vm.ExistingCourses = db.EmployeeCourses .Where(g => g.EmployeeId == id) .Select(f => new CourseVm { Id = f.Id, Name = f.Course.CourseName }).ToList(); vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray(); TempData["OriginalId"] = vm.CourseIds; vm.Courses = db.Courses.Select(f => new SelectListItem { Value = f.Id.ToString(), Text = f.CourseName }).ToList(); return View(vm); } [HttpPost] public ActionResult Edit(EditEmployeeVm model) { var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id); foreach (var couseid in newCourceIds) { db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id }); db.SaveChanges(); } return View(); } } }