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

ComboBox + Ajax + WCF = 'undefined' items in Combobox

1 Answer 87 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 16 Nov 2012, 04:19 PM
I have a REST service that outputs the following json using GET:

[{"Active":true,"DisplayText":"Flyers","TemplateCategoryID":1},{"Active":true,"DisplayText":"Door Hangers","TemplateCategoryID":2},{"Active":true,"DisplayText":"Postcards","TemplateCategoryID":3},{"Active":true,"DisplayText":"Tri-Fold","TemplateCategoryID":4},{"Active":true,"DisplayText":"Facebook Graphics","TemplateCategoryID":5}]
My Combobox setup looks like this:

@(Html.Kendo().ComboBox()  
        .Name("templateCat") //The name of the combobox is mandatory. It specifies the "id" attribute of the widget.
        .DataTextField("DisplayText") //Specifies which property of the Product to be used by the combobox as a text.
        .DataValueField("TemplateCategoryID") //Specifies which property of the Product to be used by the combobox as a value.
        .Filter(FilterType.Contains)
    .DataSource(source =>
    {
     
        source.Read(read =>
        {
            read.Action("GetTemplateCategories", "Console"); //Set the Action and Controller name         
        });
    })
    .SelectedIndex(0) //Select first item.
)
My Controller:

public ActionResult GetTemplateCategories()
{
    string baseAddress = string.Format(ConfigurationManager.AppSettings["MarketingServiceAddress"] + "GetTemplateCategories");
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
    request.Method = "GET";
    request.ContentType = "text/plain";
 
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        int statusCode = (int)response.StatusCode;
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string results = reader.ReadToEnd();
 
        
 
        return Json(results, JsonRequestBehavior.AllowGet);
    }
}
My combobox looks like it is binding to some sort of data, but all items in the list are 'undefined'.  What am I doing wrong?  I have verified that the controller is receiving the json string just fine from service.

1 Answer, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 16 Nov 2012, 09:01 PM
[SOLUTION]

So if you already have a Json string, you don't need to return JsonResults.  

Simply replace:
return Json(results, JsonRequestBehavior.AllowGet);
With:
return new ContentResult { Content = results, ContentType = "application/json" };

In hindsight, this was a 'doh' moment...
Tags
ComboBox
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Share this question
or