ahmed salah

ahmed salah

  • NA
  • 530
  • 148.7k

how to loop through hidden courses and remove from database

Sep 27 2016 12:37 PM
In my code i need to loop through hidden courses in jquery then remove it from employee course table in database sql in edit httppost under save button 
see green font in place of my code i need 
 
  1. [HttpPost]  
  2.      public ActionResult Edit(Cusomemp2 custom)  
  3.      {  
  4.   
  5.          var result = db.Employees  
  6.               .Where(p => p.Id == custom.Id)  
  7.               .Include(c => c.EmployeeCourses)  
  8.               .FirstOrDefault();  
  9.          if (custom.empcourses.Any())  
  10.          {  
  11.            //here i need to loop through data and remove hidden courses  
  12.          }  
  13.   
  14.          result.Name = custom.Name;  
  15.          
  16.   
  17.          result.EmployeeCourses = custom.empcourses;  
  18.          db.SaveChanges();  
  19.   
  20.          return View();  
  21.      }  
 
 i use following model Cusomemp2 to represent data
  1.  public class Cusomemp2  
  2.     {  
  3.         public int Id { get; set; }  
  4.         public List<EmployeeCourse> empcourses { get; set; }  
  5.   
  6.   
  7.     }  
 
in edit view model my code as following
  1. @model StudentCourses.Models.Cusomemp2  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  7.   
  8. <html>  
  9. <head>  
  10.     <meta name="viewport" content="width=device-width" />  
  11.     <title>Edit</title>  
  12.     <script src="~/scripts/jquery-1.10.2.js"></script>  
  13.     <script>  
  14.         $(function () {  
  15.               
  16.             var index = 0;  
  17.             $("#CourseId").change(function () {  
  18.   
  19.                 var id = $(this).val();  
  20.                 var txt = $("#CourseId option:selected").text();  
  21.                     $("#tb").append("<tr><td><input type = 'hidden' name='empcourses[" + index + "].CourseId' value='" + id + "'/></td><td>" + txt + "</td><td><input type='button' value='remove' class='r'</td></tr>")  
  22.                
  23.                 index++;  
  24.   
  25.             });  
  26.             $("#tb").on("click"".r"function () {  
  27.                  
  28.               $(this).parent().parent().hide();  
  29.               
  30.                   $(this).parent().prev().prev().find("input").val("0");  
  31.            
  32.              });  
  33.              
  34.   
  35.             $.ajax({  
  36.                 url: "/Employeedata/getcoursesbyempid",  
  37.                 data:{x:$("#hid").val()},  
  38.                 success: function (res) {  
  39.                     $.each(res, function (i, e) {  
  40.   
  41.   
  42.   
  43.                         $("#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>")  
  44.   
  45.   
  46.   
  47.                         index++;  
  48.                     });  
  49.                 }  
  50.   
  51.             })  
  52.         });  
  53.     </script>  
  54. </head>  
  55. <body>  
  56.     <div>  
  57.         @using (Html.BeginForm())  
  58.         {  
  59.             <div>  
  60.                 <input type="hidden" value="@ViewBag.hid" id="hid" />  
  61.                
  62.                 Name: @Html.TextBoxFor(a => a.Name)  
  63.                 <br />  
  64.                 Courses:@Html.DropDownList("CourseId")  
  65.                 <br />  
  66.                 <table id="tb"></table>  
  67.                 <input type="submit" value="save" />  
  68.   
  69.             </div>  
  70.   
  71.         }  
  72.     </div>  
  73. </body>  
  74. </html>  

see image below it show what i need
 
 
 

Answers (2)