locked
How To Update Multiple Checked Box Rows In MVC 5 RRS feed

  • Question

  • User-1766123879 posted
    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
    @using (Html.BeginForm("Update_Record", "Customer", FormMethod.Post))    
    {    
        
        
        <table id="zero_config" class="table table-striped table-bordered display">    
        
            <thead>    
                <tr>    
                    <th>    
        
                        <div class="icheck-primary">    
                            <input type="checkbox" id="Check_All" />    
        
        
                            <label for="ContractId">    
        
                            </label>    
                        </div>    
                    </th>    
                    <th>    
                        Employee Name    
                    </th>    
        
        
                    <th>Update Salary</th>    
                    <th>    
                        Date    
                    </th>    
        
        
                </tr>    
            </thead>    
            <tbody>    
                @for (int i = 0; i < Model.Count(); i++)    
                {    
                    <tr>    
                        <td>    
        
                              
                            <div class="icheck-primary">    
        
        
                                <input type="checkbox" id="ContractId" name="contId" value="@Model[i].ContractId" />    
        
        
                                <label for="ContractId">    
        
                                </label>    
                            </div>    
                        </td>    
                        <td>    
                            @Html.DisplayFor(modeli => Model[i].Customer.Name)    
                        </td>    
                          
                           
                        <td>    
                               
                            <input type="number" step="0.01" class="form-control" id="@Model[i].ContractId"    
                                   value="" name="TxtPayout" max="" />    
                               
                        </td>    
                           
        
                        <td>    
        
                            <input type="date" id="@Model[i].ContractId" value="@DateTime.Now.ToString("yyyy-MM-dd")" name="TxtDatePaid" class="form-control" />    
                        </td>    
        
        
        
                    </tr>    
                }    
            </tbody>    
        
        
        </table>    
        <input type="submit" value="Update Record" class="btn  btn-primary" />    
    }    
    My Controller [HttpPost]
    public ActionResult Update_Record(System.Web.Mvc.FormCollection form)  
          {  
      
                
              string[] contid = form.GetValues("contId");  
              decimal total = 0; // to get the total salaries  
              int cnt = 0; //Count Affected Rows  
              if(contid != null)  
              {  
                  string[] payout = form.GetValues("TxtPayout");  
                  string[] datepaid = form.GetValues("TxtDatePaid");  
                  for(int i = 0; i < contid.Length; i++)  
                  {  
                      decimal salary = Convert.ToDecimal(payout[i]);  
                      DateTime dt = Convert.ToDateTime(datepaid[i]);  
                      total += salary; //Convert.ToDecimal(payout[i]);  
                      cnt++;  
                  }  
                  TempData["Message"] = string.Format("Successfully Updated with {0} record(s) affected Total Payout: {1}", cnt, total);  
      
              }  
      
              getContract();  
              return View(ViewBag.gct);  
          }  
    If i check the entire rows and update, it works. but if i check any of the rows, it throws up error. What i want to achieve is 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 it.. Thank you for your valued time..
    Saturday, January 2, 2021 10:44 AM

Answers

  • User475983607 posted

    There are a lot of issues with your code.  I recommend going through a few beginning level tutorials before moving forward.  One of the tutorials on this site has checkbox example (around step 7) similar to your problem statement. 

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/

    Below is an example based on your sample.

    namespace MvcDemo.Models
    {
        public class CustomerViewModel
        {
            public bool Selected { get; set; }
            public int ContractId { get; set; }
            public Customer Customer { get; set; }
            public Decimal TxtPayout { get; set; }
            public DateTime TxtDatePaid { get; set; }
        }
    
        public class Customer
        {
            public string Name { get; set; }
        }
    }
    namespace MvcDemo.Controllers
    {
        public class CustomerController : Controller
        {
            // GET: Customer
            [HttpGet]
            public ActionResult Index()
            {
                List<Models.CustomerViewModel> vm = PopulateViewModel();
                return View(vm);
            }
    
            [HttpPost]
            public ActionResult Update_Record(List<Models.CustomerViewModel> models)
            {
                List<Models.CustomerViewModel> testData = new List<Models.CustomerViewModel>();
                foreach (Models.CustomerViewModel m in models)
                {
                    if(m.Selected)
                    {
                        testData.Add(m);
                    }
                }
                //Show only the selected records
                return View(testData);
            }
    
            public List<Models.CustomerViewModel> PopulateViewModel()
            {
                return new List<Models.CustomerViewModel>()
                {
                    new Models.CustomerViewModel()
                    {
                        ContractId = 1,
                        Customer = new Models.Customer() {Name="Hello"},
                        TxtDatePaid = DateTime.Now.AddDays(-3),
                        TxtPayout = 1.01m
                    },
                    new Models.CustomerViewModel()
                    {
                        ContractId = 2,
                        Customer = new Models.Customer() {Name="World"},
                        TxtDatePaid = DateTime.Now.AddDays(-2),
                        TxtPayout = 1.02m
                    },
                    new Models.CustomerViewModel()
                    {
                        ContractId = 3,
                        Customer = new Models.Customer() {Name="Foo"},
                        TxtDatePaid = DateTime.Now.AddDays(-1),
                        TxtPayout = 1.03m
                    },
                    new Models.CustomerViewModel()
                    {
                        ContractId = 4,
                        Customer = new Models.Customer() {Name="Bar"},
                        TxtDatePaid = DateTime.Now,
                        TxtPayout = 1.04m
                    }
                };
            }
        }
    }
    @model List<MvcDemo.Models.CustomerViewModel>
    @{
        ViewBag.Title = "Index";
    }
    
    <h2>Index</h2>
    @using (Html.BeginForm("Update_Record", "Customer", FormMethod.Post))
    {
    
    
        <table id="zero_config" class="table table-striped table-bordered display">
            <thead>
                <tr>
                    <th>
                        <div class="icheck-primary">
                            <input type="checkbox" id="Check_All" />
                            <label for="ContractId"></label>
                        </div>
                    </th>
                    <th>
                        Employee Name
                    </th>
                    <th>Update Salary</th>
                    <th>
                        Date
                    </th>
                </tr>
            </thead>
            <tbody>
                @for (int i = 0; i < Model.Count(); i++)
                {
                    <tr>
                        <td>
                            <div class="icheck-primary">
                                @Html.CheckBoxFor(m => Model[i].Selected)
                                @Html.HiddenFor(m => Model[i].ContractId)        
                            </div>
                        </td>
                        <td>
                            @Html.DisplayFor(modeli => Model[i].Customer.Name)
                        </td>
                        <td>
                            @Html.TextBoxFor(m => @Model[i].TxtPayout, 
                           new { @type = "number", 
                               step ="0.01", 
                               @class= "form-control",
                               max="" })
                        </td>
                        <td>
                            @Html.TextBoxFor(m => @Model[i].TxtDatePaid,
                                "{0:yyyy-MM-dd}",
                            new {
                                @class = "form-control",
                                @type="date"
                            })
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <input type="submit" value="Update Record" class="btn  btn-primary" />
    
    }    
    @model IEnumerable<MvcDemo.Models.CustomerViewModel>
    
    @{
        ViewBag.Title = "Update_Record";
    }
    
    <h2>Update_Record</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Selected)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ContractId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TxtPayout)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TxtDatePaid)
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Selected)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContractId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TxtPayout)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TxtDatePaid)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }
    
    </table>
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 2, 2021 2:39 PM