Returning JSON from a JsonResult method in MVC controller

MAB picture MAB · May 5, 2011 · Viewed 52.7k times · Source

I am trying to populate a ComboBox (Telerik RAD COmboBox) in a test ASP.NET MVC3 app.

I have defined the ComboBox on my ASPX page and in the controller I have defined the action call that returns a JsonResult.

The problem I am having is that the Web Service I am using already returns the resultset as a JSON string. How can I pass the response from the Webservice directly.

Here is the snippets of code: ASPX page:

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

Controller: called CommomController

    public JsonResult GetCALMdata()
    {
        CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
        string resultset = wsC.GetRefTables("P_1", "P_2", "P_3", "P_4");

        return ??; -- I want to return resultset which is already formatted. 
    }

Answer

alexl picture alexl · May 5, 2011

If using ASP.NET MVC 2 or higher:

return Json(resultset, JsonRequestBehavior.AllowGet);