本文介绍了从MVC控制器中的JsonResult方法返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在测试ASP.NET MVC3应用程序来填充组合框(RAD Telerik的组合框)。

我定义我的ASPX页面上的组合框,并在控制器中我已经定义了动作调用返回一个JsonResult。

我遇到的问题是,我已经使用Web Service的返回结果为JSON字符串。我怎样才能通过直接从web服务的响应。

下面是code的片段:
ASPX页面:
<%Html.Telerik()组合框()
                       。名称(cbRefTables)
                       .DataBinding(B =>乙
                           阿贾克斯()
                           。选择(GetCALMdata,通用)结果
                       )
                       .Render();
                %>

控制器:所谓CommomController

 公共JsonResult GetCALMdata()
    {
        CALMwsP.wsCALMSoapClient WSC =新CALMwsP.wsCALMSoapClient(wsCALMSoap);
        串结果集= wsC.GetRefTables(P_1,P_2,P_3,P_4);        返回??; - 我想返回已格式化的结果集。
    }


解决方案

如果在的ResultSet 字符串已经是JSON(而不是包裹在任何XML),那么你要返回 ContentResult类型带正是字符串内容:

 公共ContentResult类型GetCALMdata()
{
    CALMwsP.wsCALMSoapClient WSC =新CALMwsP.wsCALMSoapClient(wsCALMSoap);
    串结果集= wsC.GetRefTables(P_1,P_2,P_3,P_4);    返回的内容(结果集,应用/ JSON);
}

您不想使用 JsonResult JSON()助手在这种情况下,因为这是怎么回事落得重新序列化的JSON。

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.
    }
解决方案

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.

这篇关于从MVC控制器中的JsonResult方法返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:24
查看更多