Returning JSON from a JsonResult method in MVC controller
Asked Answered
P

5

16

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. 
    }
Pimiento answered 5/5, 2011 at 16:53 Comment(0)
D
21

If the resultset string is already JSON (and not wrapped in any XML), then you'd want to return a ContentResult with exactly that string as the content:

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");
}

You don't want to use JsonResult or the Json() helper in this case, because that's going to end up re-serializing your JSON.

Dependency answered 5/5, 2011 at 17:44 Comment(1)
The additional contentType parameter being set to application/json is what I needed. I was doing the Json serialization with Newtonsoft.Json rather than the built in one because I wanted some of serialization customisation it can do.Tonry
A
29

If using ASP.NET MVC 2 or higher:

return Json(resultset, JsonRequestBehavior.AllowGet);
Anissaanita answered 5/5, 2011 at 17:1 Comment(4)
That is what I originally had, however I get the following error: Microsoft JScript runtime error: 'Selected' is null or not an objectPimiento
Well, that error isn't coming out of the controller. So your issue is with how you are handling the JSON.Pitch
most likely that error is from the combobox, it doesn't know what the default selected item should beLampion
What is the using reference?Ely
D
21

If the resultset string is already JSON (and not wrapped in any XML), then you'd want to return a ContentResult with exactly that string as the content:

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");
}

You don't want to use JsonResult or the Json() helper in this case, because that's going to end up re-serializing your JSON.

Dependency answered 5/5, 2011 at 17:44 Comment(1)
The additional contentType parameter being set to application/json is what I needed. I was doing the Json serialization with Newtonsoft.Json rather than the built in one because I wanted some of serialization customisation it can do.Tonry
N
5

if I correctly understood you should use the Json() method

return Json(resultset);
Ninfaningal answered 5/5, 2011 at 16:58 Comment(1)
What is the using reference?Ely
H
1

The individual Json Method:

return Json(resultset);

It needs the System.Web.Http DLL and the namespace is System.Web.Http.Results.

enter image description here


Or Website wide put this line in the WebApiConfig.cs

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
Hassi answered 12/6, 2018 at 4:25 Comment(1)
+ for including DLL name and namespace.Shellyshelman
B
1

In MVC 5 and possibly below you can do something like this:

            var dict = new Dictionary<string, string>
            {
                { "name", "Foobar" },
                { "url", "[email protected]" }
            };

            var json = new JsonResult()
            {
                Data = dict
            };
Bourke answered 1/1, 2020 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.