Returning JSON from a JsonResult method in MVC controller

后端 未结 5 1260
温柔的废话
温柔的废话 2021-01-04 18:06

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 define

相关标签:
5条回答
  • 2021-01-04 18:22

    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.

    0 讨论(0)
  • 2021-01-04 18:28

    If using ASP.NET MVC 2 or higher:

    return Json(resultset, JsonRequestBehavior.AllowGet);
    
    0 讨论(0)
  • 2021-01-04 18:35

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

    return Json(resultset);
    
    0 讨论(0)
  • 2021-01-04 18:40

    The individual Json Method:

    return Json(resultset);

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


    Or Website wide put this line in the WebApiConfig.cs

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    
    0 讨论(0)
  • 2021-01-04 18:41

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

                var dict = new Dictionary<string, string>
                {
                    { "name", "Foobar" },
                    { "url", "admin@foobar.com" }
                };
    
                var json = new JsonResult()
                {
                    Data = dict
                };
    
    0 讨论(0)
提交回复
热议问题