get list of selected items from the ListView

6 posts, 0 answers
  1. Dimitri
    Dimitri avatar
    1 posts
    Member since:
    Jul 2013

    Posted 13 Aug 2013 Link to this post

    Hello all,

    I've been playing around with the Kendo UI controls for a while now and for the most part, it's going great :) (otherwise I wouldn't be on the forums)

    When working with the listview-control (configured in multi-select-mode) I can't get the list of selected items.

    Code:

    01.@using (Html.BeginForm("MyMethod", "MyController",Model)) 
    02.{  
    03.        @(Html.Kendo().ListView<List<MyObject>>() 
    04.              .Name("ListOfObjects") 
    05.               .DataSource(datasource => datasource.Read(read => read.Action("GetMyListOfObjects", "MyController")))                   
    06.                .Selectable(select => select.Mode(ListViewSelectionMode.Multiple)) 
    07.                .ClientTemplateId("selectedItems") 
    08.                .TagName("div") 
    09.               .Events(e => e.Change("onSelectObject")) 
    10.                            )  
    11.<input type="submit" class="submit" value="Search" /> 
    12.}
    Controller:
    1.public ActionResult GetMyListOfObjects([DataSourceRequest]DataSourceRequest request)
    2.{
    3.    return Json(MyService.GetMyListOfObjects().ToDataSourceResult(request));
    4.}

    To check if I actually selected something, I added an event (the change event "onSelectObject):

    function onSelectObject(e) {
            var selected = $.map(this.select(), function (item) {
                return $(item).text();
            });
              
            alert("Selected: " + selected.length + " item(s), [" + selected.join(", ") + "]");
              
            return selected;        
        }

    This works. Meaning that when I select items in my listview, the event is fired and I see the alert-message with the correct length and list of selected items.


    Now my question is, when I click the "search" button in the form above, how do I get the list of selected items in my controller?

    1.public ActionResult MyMethod(List<MyObject> selectedObjects)
    2.{
    3.     //...selectedObjects is always null
    4.    return View();
    5.}
  2. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2284 posts

    Posted 15 Aug 2013 Link to this post

    Hello Dimitri,

    You can send additional data to the server via Data method of the Read action. The code for the `Data` function can be similar to what is for the `change` handler.

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Narender
    Narender avatar
    5 posts
    Member since:
    Jan 2013

    Posted 05 Nov 2013 Link to this post

    Hi Nikolay, I do have a similar requirement of posting the selected and unselected checkbox items to Post action method of the controller.
    Can you please provide me any working example that you have...Thanks Naren...
  5. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2284 posts

    Posted 06 Nov 2013 Link to this post

    Hello Narender,

    We don't have example demonstrating exactly what you need, but the documentation states the general way to implement it. What issues you are facing while implementing it?  

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Narender
    Narender avatar
    5 posts
    Member since:
    Jan 2013

    Posted 06 Nov 2013 Link to this post

    Hi Nikolay...Thanks for the response.i just want to know the usage of Change for posting data to controller..as you suggested Dimitri.

    The issue here is i couldnt send the checked/unchecked items to the controller's post method..which will used for business processing.

    As you suggested to Dimitri, do you mean using change handler as we use data method handler?

    the change handler is on the client side right..how can these values be posted to controllers post method?

    Thanks,
    Naren
  7. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2284 posts

    Posted 08 Nov 2013 Link to this post

    Hello Naren,

    Data handler of the Read action is intended to be used to send additional data to server. I.e when you define:
    .DataSource(dataSource => {
            dataSource.Read(read => read.Action("ActionMethod", "Controller").Data("functionName"));
    })

    that `functionName`  function will be called and you can gather all the data (in your scenario selected checkboxes) and return them. They will be send altogether with the other DataSource data on server. The link in the docs show how it will received on server.

    Change event will be triggered every time selection occur. You can use it to track changes in the selection and apply your business logic.

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top
Kendo UI is VS 2017 Ready