4
Answers

How to remove values in ids variable from EmployeeCourse tab

ahmed salah

ahmed salah

8y
310
1
Problem in server side
I need to remove values found in string ids variable under Edit HTTP Post 
from EmployeeCourse table
  1. public ActionResult Edit(FormCollection form)    
  2. {    
  3. string ids = form["ids"];    
  4. // Remove values found in ids variable from EmployeeCourse table in database  
  5. return View();    
  6. }         
 The processes of  my application is
 
1- get courses for every employee from EmployeeCourse table column CourseId in Edit get
  1. $.ajax({  
  2.                 url: "/Employeedata/getcoursesbyempid",  
  3.                 data:{x:$("#hid").val()},  
  4.                 success: function (res) {  
  5.                     $.each(res, function (i, e) {  
  6.   
  7.                         $("#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>")  
  8.   
  9.                         index++;  
  10.                     });  
  11.                 }  
  12.   
  13.             })  
  14.         });  
  15. public JsonResult getcoursesbyempid(int x)  
  16.         {  
  17.             db.Configuration.ProxyCreationEnabled = false;  
  18.             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 });  
  19.             return Json(data, JsonRequestBehavior.AllowGet);  
  20.         }  
 
2-If i remove courses then it will store in text box ids as 1,2.
 
The value stored in string ids variable get it from text box name ids found in view Edit get This text box store removed courses as 1,2 and after this click save button to remove from EmployeeCourse table in database.
To get values removed in text box name ids i do as following
 
  1. $("#tb").on("click"".r"function () {        
  2. $(this).parent().parent().hide();        
  3. $(this).parent().prev().prev().find("input").addClass("remove");        
  4. var ids = [];        
  5. var i = 0;        
  6. $(".remove").each(function () {        
  7. ids[i] = $(this).val();        
  8. i++;        
  9. });        
  10. $("#ids").val(ids);        
  11. });        
  12. @using (Html.BeginForm("Edit", "Employeedata", FormMethod.Post))        
  13. {        
  14. <div>        
  15. <table id="tb"></table>        
  16. <input type="submit" value="save" />        
  17. <input type="text" name="ids" id="ids" />        
  18. </div>        
  19. <table id="tb">        
  20.         
  21. </table>        
  22. }  
3-after this i will click save to remove course 1 and 2 as example from EmployeeCourse table 
Process No 3 this is actually what i need 
  1. public ActionResult Edit(FormCollection form)      
  2. {      
  3. string ids = form["ids"];      
  4. // Remove values found in ids variable from EmployeeCourse table in database    
  5. return View();      
  6. }    
 
The image below have details of EmployeeCourse Table
 

Answers (4)