pius joshua

pius joshua

  • 1.6k
  • 29
  • 5.7k

How To Update Multiple Checked Box Rows In MVC 5

Jan 2 2021 10:39 AM
Hi, Happy New Year All. I am having this issues with updating selected  input textbox in a table rows.
Here is my Code
My View Page
 
 
  1. @using (Html.BeginForm("Update_Record", "Customer", FormMethod.Post))    
  2. {    
  3.     
  4.     
  5.     <table id="zero_config" class="table table-striped table-bordered display">    
  6.     
  7.         <thead>    
  8.             <tr>    
  9.                 <th>    
  10.     
  11.                     <div class="icheck-primary">    
  12.                         <input type="checkbox" id="Check_All" />    
  13.     
  14.     
  15.                         <label for="ContractId">    
  16.     
  17.                         </label>    
  18.                     </div>    
  19.                 </th>    
  20.                 <th>    
  21.                     Employee Name    
  22.                 </th>    
  23.     
  24.     
  25.                 <th>Update Salary</th>    
  26.                 <th>    
  27.                     Date    
  28.                 </th>    
  29.     
  30.     
  31.             </tr>    
  32.         </thead>    
  33.         <tbody>    
  34.             @for (int i = 0; i < Model.Count(); i++)    
  35.             {    
  36.                 <tr>    
  37.                     <td>    
  38.     
  39.                           
  40.                         <div class="icheck-primary">    
  41.     
  42.     
  43.                             <input type="checkbox" id="ContractId" name="contId" value="@Model[i].ContractId" />    
  44.     
  45.     
  46.                             <label for="ContractId">    
  47.     
  48.                             </label>    
  49.                         </div>    
  50.                     </td>    
  51.                     <td>    
  52.                         @Html.DisplayFor(modeli => Model[i].Customer.Name)    
  53.                     </td>    
  54.                       
  55.                        
  56.                     <td>    
  57.                            
  58.                         <input type="number" step="0.01" class="form-control" id="@Model[i].ContractId"    
  59.                                value="" name="TxtPayout" max="" />    
  60.                            
  61.                     </td>    
  62.                        
  63.     
  64.                     <td>    
  65.     
  66.                         <input type="date" id="@Model[i].ContractId" value="@DateTime.Now.ToString("yyyy-MM-dd")" name="TxtDatePaid" class="form-control" />    
  67.                     </td>    
  68.     
  69.     
  70.     
  71.                 </tr>    
  72.             }    
  73.         </tbody>    
  74.     
  75.     
  76.     </table>    
  77.     <input type="submit" value="Update Record" class="btn  btn-primary" />    
  78. }   
 
 My Controller [HttpPost]
 
  1. public ActionResult Update_Record(System.Web.Mvc.FormCollection form)  
  2.       {  
  3.   
  4.             
  5.           string[] contid = form.GetValues("contId");  
  6.           decimal total = 0; // to get the total salaries  
  7.           int cnt = 0; //Count Affected Rows  
  8.           if(contid != null)  
  9.           {  
  10.               string[] payout = form.GetValues("TxtPayout");  
  11.               string[] datepaid = form.GetValues("TxtDatePaid");  
  12.               for(int i = 0; i < contid.Length; i++)  
  13.               {  
  14.                   decimal salary = Convert.ToDecimal(payout[i]);  
  15.                   DateTime dt = Convert.ToDateTime(datepaid[i]);  
  16.                   total += salary; //Convert.ToDecimal(payout[i]);  
  17.                   cnt++;  
  18.               }  
  19.               TempData["Message"] = string.Format("Successfully Updated with {0} record(s) affected Total Payout: {1}", cnt, total);  
  20.   
  21.           }  
  22.   
  23.           getContract();  
  24.           return View(ViewBag.gct);  
  25.       } 
 If i check the entire row and update, it works. but if i check any of the rows, it throws up error. What i want to achieve it to get the input values of only the rows i check instead of getting the entire input values of the input text boxes. Kindly Assist me with.. Thanks for your valued time..
 

Answers (1)