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

Binding ComboBox to JSON string returned by external Web Service

4 Answers 54 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
michael
Top achievements
Rank 2
michael asked on 05 May 2011, 02:02 PM
Hi,

I am trying to populate a ComboBox with a list in JSON format that is returned by an external web Service.

This is the code I have

On the aspx page:
               <% Html.Telerik().ComboBox()
                       .Name("cbRefTables")
                       .DataBinding(b => b
                           .Ajax()
                           .Select("GetCALMdata","Common")                    
                       )
                       .Render();
                %>

In my controller:

        public JsonResult GetCALMdata()
        {
            CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
            return Json(wsC.GetRefTables("MyID", "MyDB", "MyWorkstation", "MyApp"), JsonRequestBehavior.AllowGet);
        }

I get the following error:

Microsoft JScript runtime error: 'Selected' is null or not an object

4 Answers, 1 is accepted

Sort by
0
michael
Top achievements
Rank 2
answered on 05 May 2011, 10:22 PM
Further to my last post, I have realized that since my data was being returned in JSON, I did not need to re-serialize it!

as such the controller code not looks like this:

        public ContentResult GetCALMdata()
        {
            CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
            string resultset = wsC.GetRefTables("P_1", "P_2", "P_3", "P_4");
            return Content(resultset, "application/json");
        }

and my ASPX code like this:

                <% Html.Telerik().ComboBox()
                       .Name("cbRefTables")
                       .DataBinding(b => b
                           .Ajax()
                           .Select("GetCALMdata","Common")                    
                       )
                       .SelectedIndex(1)
                       .Render();
                %>
 Telerik.common.min.js now throws the following error:

Microsoft JScript runtime error: 'undefined' is null or not an object

with the following highlighted:
}else{if(e.selectionStart!==undefined){k=e.selectionStart}}return k},encode:function(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\u00a0/g,"&nbsp;")},

 
0
Kalina
Telerik team
answered on 11 May 2011, 09:43 AM
Hi michael,

I suppose that you use Telerik ComboBox for ASP.NET MVC.
Could you please take a look at this online demo?

Regards,
Kalina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
michael
Top achievements
Rank 2
answered on 11 May 2011, 01:08 PM
Hi,

Thanks for the reply...

I had looked at the demo you suggested, and I used it to construct my example.

The issue I have is that in the example, the resultset is converted into a JSON string and the TEXT and VALUE fields are specified:                               return new JsonResult { Data = new SelectList(products.ToList(), "ProductID","ProductName") };

In my case, since the Web Service already returns a JSON string, I did not see the need to de-serialize it and then re-serialize it.

I was hoping to be able to bind the data returned (CODE and DESCRIPTION) in the JSON string directly; something like:
                   EmptyMessage="Select a Company"
                   DataTextField="DESCRIPTION"
                   DataValueField="CODE"


Hope you understand what I am trying to do.

Thanks for the assistance.

Mike B.
0
Georgi Krustev
Telerik team
answered on 12 May 2011, 06:29 AM
Hello Michael,

ComboBox/DropDownList UI component is designed to work with DropDownList object ( which is similar to SelectListItem). Hence they expects to be bound on the client to list of JSONs, which have structure like this:

{Text: "Text", Value:"1"}

If you need to bind the combobox to JSON with different structure, you will need to convert it to the one mentioned above. For instance you can use this code snippet:
function getList(data){
    var list = $.map(data, function(item) { return {TEXT: item.DESCRIPTION, VALUE: item.CODE }; });
    list.splice(0,0, {TEXT: "Select company...", VALUE:"-1"});
    return list;
}

Kind regards,
Georgi Krustev
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
michael
Top achievements
Rank 2
Answers by
michael
Top achievements
Rank 2
Kalina
Telerik team
Georgi Krustev
Telerik team
Share this question
or