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

get list of selected items from the ListView

5 Answers 585 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Dimitri
Top achievements
Rank 1
Dimitri asked on 13 Aug 2013, 07:26 AM
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.}

5 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 15 Aug 2013, 07:19 AM
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!
0
Narender
Top achievements
Rank 1
answered on 06 Nov 2013, 05:10 AM
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...
0
Nikolay Rusev
Telerik team
answered on 06 Nov 2013, 02:00 PM
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!
0
Narender
Top achievements
Rank 1
answered on 06 Nov 2013, 03:31 PM
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
0
Nikolay Rusev
Telerik team
answered on 08 Nov 2013, 12:14 PM
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!
Tags
ListView
Asked by
Dimitri
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Narender
Top achievements
Rank 1
Share this question
or