We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

using foreach to save data not save courses correctly

Thread ID:





125695 Sep 3,2016 11:12 PM UTC Sep 5,2016 03:17 PM UTC ASP.NET MVC 1
Tags: General
Asked On September 3, 2016 11:12 PM UTC


save name in employee table and save all courses

in employee courses table when click submit button in edit [HTTPPOST]

Problem summary

submit button save changes only and delete data exist before

Problem details

when click submit button in edit post .courses will save

what i added or selected courses before click submit it will save

but courses that employee have before will delete


1- i add employee name MEDO have course c++

2-in edit view it show c++

3-if i add python in edit view then click submit it must have two courses

c++ and python

4- it show to me python only when open edit for this employee again

it save courses selected but courses exist before deleted

Image for problem

image found below show all details

wrong result when update data of employee course
SAVE all courses in employeecourse table


public class EditEmployeeVm
public int Id { set; get; }
public string Name { get; set; }
public List<SelectListItem> Courses { get; set; }
public int[] CourseIds { set; get; }
public List<CourseVm> ExistingCourses { set; get; }

public class CourseVm
public int Id { set; get; }
public string Name { set; get; }
in edit function get i pass data to edit 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

vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();
vm.Courses = db.Courses.Select(f => new SelectListItem
Value = f.Id.ToString(),
Text = f.CourseName

return View(vm);

public ActionResult Edit(EditEmployeeVm model)
var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
foreach (EmployeeCourse eec in emp.EmployeeCourses.ToList())
var ec = db.EmployeeCourses.Find(eec.Id);

foreach (var couseid in model.CourseIds)
db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });

return View();

Prince Oliver [Syncfusion]
Replied On September 5, 2016 03:17 PM UTC

Hi Ahmedsa, 

Thanks for contacting Syncfusion support. 

We are currently validating your requirement and update you on 6th September. We appreciate your patience until then. 



This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon