This is a migrated thread and some comments may be shown as answers.

How to pass all "checkbox" rows to the controller

1 Answer 349 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 30 May 2016, 04:46 AM

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);
}

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 31 May 2016, 11:07 AM
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
Tags
Grid
Asked by
Bill
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or