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

Binding data source to webservice that returns json serialized dicationary<string,string>

3 Answers 477 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Patt
Top achievements
Rank 1
Patt asked on 03 Aug 2012, 01:42 PM
Hi everyone,

I am trying to create a kendo dropdownlist that binds to a datasource from a web service.

Since I want to reduce the size of the sent data, I decide to use dictionary<string, string> instead of  a class that has properties for the select name and value.

However, I got a bit stuck on the mapping part since the dictionary<string, string> will convert to Json string as { "value1" : "name1", "value2" : "name2", ...}, hence javascript will see value{n} as properties and name{n} as their values instead of pairs of two separate value.

Bottom line is that I would like to convert these pairs of values into datasource fields "name", and "value". What would be the best way to tackle this?

Thank you,

Patt

3 Answers, 1 is accepted

Sort by
0
Accepted
John DeVight
Top achievements
Rank 1
answered on 06 Aug 2012, 10:02 PM
Hi Patt,

I would probably use jQuery.ajax to get the data from the web service first, then format it as an array of json objects where each json object uses "name" and "value", and finally bind the array to the dropdownlist.  No need for the dataSource object.

Here is an example of a jQuery.ajax call that gets a serialized dictionary<string, string>, builds a new array, and then binds the array to a kendoDropDownList:

$.ajax({
    url: _rootUrl + "Home/GetDictionaryForDropdown",
    cache: false
})
.done(function (e) {
    var data = [];
    for (var key in e) {
        data.push({ "name": key, "value": e[key] });
    };
  
    $("#mydropdownlist").kendoDropDownList({
        dataTextField: "name",
        dataValueField: "value",
        dataSource: data
    });
});

Hopefully that helps.

Regards,

John DeVight
0
Patt
Top achievements
Rank 1
answered on 07 Aug 2012, 12:07 AM
That was perfect! Thank you :)
0
John DeVight
Top achievements
Rank 1
answered on 07 Aug 2012, 11:46 AM
Your welcome.  When you get a chance, can you mark my response as the answer?  Thank you :)
Tags
Data Source
Asked by
Patt
Top achievements
Rank 1
Answers by
John DeVight
Top achievements
Rank 1
Patt
Top achievements
Rank 1
Share this question
or