Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Problem

when remove course then click submit not save changes although

it removed from client side by jquery

Details

in edit view for every employee i need to do changes by remove or add new courses for

employee

if i add new courses then click submit button it save what i do

but if i remove course from courses then click submit it will not save courses i

removed

so that i need to check what is wrong in my code

my code working without any problem but only have problem

i cannot save courses removed in database in employeecourse table when click submit

employeecourse table have Id,EmployeeId,CourseId

jquery remove client side attached with my question

code

C#
[HttpPost]  
                    public ActionResult Edit(EditEmployeeVm model)  
                    {  
                        var emp = db.Employees.FirstOrDefault(f => f.Id == model.Id);  
                        foreach (var couseid in model.CourseIds)  
                        {  
                            db.EmployeeCourses.Add(new EmployeeCourse { CourseId = couseid, EmployeeId = emp.Id });  
                            db.SaveChanges();  
                        }  
                    
                        return View();  
            my(custom model) view model using for that  
             public class EditEmployeeVm  
                {  
                    public int Id { set; get; }  
                    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; }  
                }  
            }

to see what i need in image below
Simple File Sharing and Storage.[^]

What I have tried:

cannot save changes of courses if remove in edit httppost
Posted
Updated 18-Feb-22 8:11am
v8
Comments
Philippe Mori 10-Sep-16 18:13pm    
Please make your question readable. Youi won't get much help with unformatted code.

Add some blank lines in your text. Start your sentence with capital letter. Add a dot at the end of your sentence. It is very hard to read your question.

If you don't put effort in your question, we won't try hard to answer you.

1 solution

I'm hoping you just forgot to post your delete action in your controller. But in the likelihood that you didn't, the reason why your item isn't deleting is because you have no delete code.

Since i know nothing about your html/javascript I'm going to assume you know how to make ajax calls. To throw this code in your edit action is a bad idea and would be confusing, that is unless you are trying to "Stage" the delete prior to pressing your submit button. But again, I have no idea what you are trying to do so i'll give two options.

1) Add another action to your controller for deletes.

C#
[HttpPost]
public JsonResult Delete(int courseId)
{
   var course = context.Courses.FirstOrDefault(m=>m.Id == courseId);
   context.Courses.Remove(course); // this may be wrong, it may be context.Remove(course)...i use a repository class so I can unit test my classes so its been a while for me to write straight EF db access code like this.

    context.SaveChanges();
   return Json(new { }, JsonRequestBehavior.AllowGet);
}


2) If you are trying to stage the delete once submit has happened then you can do 2 things. Can can continuously delete and re-add everything. Or you can keep a hidden area in your HTML that keeps a running total of your course id's to delete/remove. This can be placed into your Edit action of your controller

So the first thing you'd do is have something like this, hidden in your html

HTML
<div id="things-to-hide">

</div>


Then on click of the remove, you would add a hidden input to the things-to-hide div that keeps a running tally of ID's to remove.

Something like

JavaScript
// use .on instead of .click, i'm lazy and don't feel like it, although for the time it took me to type this i could have done .on
$(".remove").click(function() {
    // this is assuming you are attaching the id to the remove link as another attribute called data-id, EX:    <a href="" data-id="2">Remove</a>
   var idToRemove = $(this).data("id");
   
// i typed straight into codeproject, syntax may be a little off but you get the idea
   $("#things-to-hide").append("<input type="hidden" value=""+idToRemove+"" name="removeid" />");


});


Then in your model you need to add another property of an array of int's so your form submit, whether its Html.BeginForm or Ajax.BeginForm, can auto map your array of removeid to a property on your model.

So something like

C#
public class EmployeeModel
{
   public int[] removeid  {get;set;}
}



Then in your controller, you can do something similar to what i posted in option 1 which is loop over the removeid array, which should contain the id's that are deisred to be remove, and leave the courses alone that you want to keep.

So it would look something like


C#
[HttpPost]
public ActionResult Edit(EmployeeModel model)
{
   foreach(var id in model.removeid)
   {
      var course = context.Courses.FirstOrDefault(m=>m.Id == courseId);
      context.Courses.Remove(course); // this may be wrong, it may be       context.Remove(course)...i use a repository class so I can unit test my classes so its been a while for me to write straight EF db access code like this.

      context.SaveChanges();
   }
}


Hopefully this points you in the right direction. This may not be a one size fits all so i encourage you to look at what i've submitted and massage it to fit your needs. But in general, this should fit what you are looking for.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900