Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
In edit HTTP post i need to remove courses stored in ids variable

note i need remove courses selected not edit


http://www.mediafire.com/view/awe5t3xlh6ghbgx/remove_ids_courses.jpg[^]

in jquery i store values of removed courses in ids variable

suppose i removed photoshop and flash it will store value of 3,4 in ids variable

below code when click remove it save values of courses removed in variable ids as

array

JavaScript
$("#tb").on("click", ".r", function () {  
$(this).parent().parent().hide();  
$(this).parent().prev().prev().find("input").addClass("remove");  
var ids = [];  
var i = 0;  
$(".remove").each(function () {  
ids[i] = $(this).val();  
i++;  
});  
for (var k = 0; k < ids.length; k++) {  
alert(ids[k]);  
}  
});  


i show courses per employee from table EmployeeCourse in edit get as following

JavaScript
var index = 0;  
$.ajax({  
url: "/Employeedata/getcoursesbyempid",  
data:{x:$("#hid").val()},  
success: function (res) {  
$.each(res, function (i, e) {  
$("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + e.Id + "'/></td><td>" + e.CourseName + "</td><td><input type='button' value='remove' class='r'/></td></tr>")  
index++;  
});  
}  
})  
public JsonResult getcoursesbyempid(int x)  
{  
db.Configuration.ProxyCreationEnabled = false;  
var data = db.EmployeeCourses.Where(a => a.EmployeeId == x).Join(db.Courses, a => a.CourseId, b => b.Id, (a, b) => new { Id = a.CourseId, CourseName = b.CourseName });  
return Json(data, JsonRequestBehavior.AllowGet);  
} 


my mode using as following

C#
public class Cusomemp2  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public List<EmployeeCourse> empcourses { get; set; }  
}  


when remove courses in edit HTTPPOST what i write here

C#
[HttpPost]  
public ActionResult Edit(Cusomemp2 custom)  
{  
//what i write to remove courses saved in ids from table EmployeeCourse  
return View();  
}  


What I have tried:

how to remove courses store in ids in edit post controller
Posted
Updated 8-Oct-16 4:17am

1 solution

I'm not terribly clear on everything you've laid out but to simply delete courses from your course table you need to do something like

C#
[HttpPost]  
public ActionResult Edit(Cusomemp2 custom)  
{  
//what i write to remove courses saved in ids from table EmployeeCourse  

// I think you are storing the ID's of courses to remove in a string. It isn't clear from your model how that is stored so I'm going to fake it here.

var idsToRemove = "1,2";
var splitIds = idsToRemove.Split(',');

foreach(var id in splitIds)
{
    var delete = context.EmployeeCourse.FirstOrDefault(m=>m.EmployeeId == custom.EmployeeId && m.CourseId == id);
    if(delete != null)
    {
        context.EmployeeCourse.Remove(delete);
    }
}

context.SaveChanges();

return View();  
}  
 
Share this answer
 
Comments
ahmed_sa 8-Oct-16 16:02pm    
only i need to ask how to get values of removed courses stored in ids variable from view to controller under edit http post action
edit http post action represent what i write when i click save to remove courses
selected from EmployeeCourse table in database

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