How to pass all "checkbox" rows to the controller

2 posts, 0 answers
  1. Bill
    Bill avatar
    1 posts
    Member since:
    Feb 2016

    Posted 29 May Link to this post

    I am wanting to pass only the rows where the checkbox is checked to my controller but I can not seem to figure this out.  I don't want to use selected but checked boxes.

    Scenario: user is presented with a list of products, some of which are pre-checked.  they will then select more products by checking the checkboxes.  I want to send all those rows to the controller, and also have it work with paging.  Is there a way to change the following from using "selected" to use the checked?

     

    // gather the selected rows and send them to the controller
      $(function () {
          $('#btnGenerate').click(function () {
              var grid = $('#products').data('kendoGrid');
              var selectedElements = grid.select();
              for (var j = 0; j < selectedElements.length; j++) {
                  var item = grid.dataItem(selectedElements[j]);
                  items['products[' + j + '].ItemDescription'] = item.ItemDescription;
                  items['products[' + j + '].Quantity'] = item.Quantity;
              }
              $.ajax({
                  type: "POST",
                  data: items,
                  url: '@Url.Action("ProductsCart","Trial")',
                  success: function () {
                      window.location.href = "@Url.Action("ProductsCart", "Trial")";
                  },
                  error: function () {
                      alert("No products were selected.");
                  }
              });
          });
      });

    // POST: Add Trial Products to cart
    [HttpPost]
    public ActionResult ProductsCart(TrialProductViewModel[] products)
    {
       // do something with products
     
        return Json(products);
    }

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 31 May Link to this post

    Hello Bill,

    In order to implement the behavior you can iterate through the items in the grid and add the ones where the checkbox is checked. Then you can pass the collection to the server as you are currently doing.

    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top