You must Sign In to post a response.
  • Category: ASP.Net MVC

    Using foreach to save data not save courses correctly(delete previous courses

    Needs

    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

    Example

    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

    http://www.mediafire.com/view/m4f008mtnnkyodv/wrong_save.jpg

    SAVE all courses in employeecourse table

    Code

    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
    }).ToList();

    vm.CourseIds = vm.ExistingCourses.Select(g => g.Id).ToArray();
    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 (EmployeeCourse eec in emp.EmployeeCourses.ToList())
    {
    var ec = db.EmployeeCourses.Find(eec.Id);
    db.EmployeeCourses.Remove(ec);
    db.SaveChanges();
    }

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

    return View();
    }
  • #767747
    Hai Ahmdesa,
    Looks like below code is having issue:

    foreach (EmployeeCourse eec in emp.EmployeeCourses.ToList())
    {
    var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);
    var ec = db.EmployeeCourses.Find(eec.Id);
    db.EmployeeCourses.Remove(ec);
    db.SaveChanges();
    }

    Here in the code, you are finding the each course and removing it for that employee. That is the reason, all the courses are removed for the existing one.
    You can just comment the above code and only use the below code.

    [HttpPost]
    public ActionResult Edit(EditEmployeeVm model)
    {
    foreach (var couseid in model.CourseIds)
    {
    db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
    db.SaveChanges();
    }
    return View();
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767773
    OK it working but if i using remove to course by jquery it remove item from interface but not save changing in database

  • #767774
    i using that in remove button to course
    <script>
    $(function () {
    $(document).on("click", ".remove", function (e) {
    e.preventDefault();
    $(this).closest(".course-item").remove();
    });
    $('#AvailableCourses').change(function () {
    var val = $(this).val();
    var text = $("#AvailableCourses option:selected").text();
    var existingCourses = $("input[name='CourseIds']")
    .map(function () { return this.value; }).get();

    if (existingCourses.indexOf(val) === -1) {
    // Not exist. Add new
    var newItem = $("<div/>").addClass("course-item")
    .append(text + ' <a href="#" class="remove" data-id="' + val + '">Remove </a>');
    newItem.append('<input type="text" name="CourseIds" value="' + val + '" />');

    $("#items").append(newItem);
    }
    });
    });
    </script>
    </head>
    <body>
    <div>
    @using (Html.BeginForm())
    {

    @Html.HiddenFor(g => g.Id)
    @Html.DropDownList("AvailableCourses", Model.Courses, "Select")
    <div id="items"></div>
    foreach (var c in Model.ExistingCourses)
    {
    <div class="course-item">
    @c.Name <a href="#" class="remove" data-id="@c.Id">Remove </a>
    <input type="text" name="CourseIds" value="@c.Id" />
    </div>
    }

    }
    </div>
    </body>
    </html>

  • #767795
    Hi,

    In the below piece of code why are you remove items from Course Table


    [HttpPost]
    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);
    db.EmployeeCourses.Remove(ec);
    db.SaveChanges();
    }

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

    return View();
    }


    you have to remove the below piece of code and retry it again

    foreach (EmployeeCourse eec in emp.EmployeeCourses.ToList())
    {
    var ec = db.EmployeeCourses.Find(eec.Id);
    db.EmployeeCourses.Remove(ec);
    db.SaveChanges();
    }


    before that you have to know your exact requirement if you want to update your details then don't do remove/ delete items just update the items that is enough. But as i seen in your code first you remove items from your table and re add it again, that is wrong.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767806
    i using the following code only
    foreach (var couseid in model.CourseIds)
    {
    db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });
    db.SaveChanges();
    }

    return View();
    }
    but if i remove course from interface then click submit not save changes of remove in database
    meaning same course i remove before already exist in database

    Delete Attachment


Sign In to post your comments