从 MVC 控制器中的 JsonResult 方法返回 JSON
我正在尝试在测试 ASP.NET MVC3 应用程序中填充 ComboBox (Telerik RAD COmboBox)。
我已经在 ASPX 页面上定义了 ComboBox,并在控制器中定义了返回 JsonResult 的操作调用。
我遇到的问题是我正在使用的 Web 服务已经将结果集作为 JSON 字符串返回。如何直接传递来自 Webservice 的响应。
这是代码片段: ASPX 页面:
<% Html.Telerik().ComboBox()
.Name("cbRefTables")
.DataBinding(b => b
.Ajax()
.Select("GetCALMdata","Common")
)
.Render();
%>
控制器:称为 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.
}
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.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果使用 ASP.NET MVC 2 或更高版本:
If using ASP.NET MVC 2 or higher:
如果
resultset
字符串已经是 JSON(并且未包装在任何 XML 中),那么您希望返回一个ContentResult
,其中该字符串恰好作为内容:您不需要在这种情况下,不想使用
JsonResult
或Json()
帮助器,因为这最终会重新序列化您的 JSON。If the
resultset
string is already JSON (and not wrapped in any XML), then you'd want to return aContentResult
with exactly that string as the content:You don't want to use
JsonResult
or theJson()
helper in this case, because that's going to end up re-serializing your JSON.如果我正确理解你应该使用 Json() 方法
if I correctly understood you should use the Json() method
单个 Json 方法:
return Json(resultset);
它需要 System.Web.Http DLL,命名空间为 System.Web.Http.Results。
或者网站范围内将此行放入 WebApiConfig.cs 中
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
在 MVC 5 或以下版本中,您可以执行以下操作:
In MVC 5 and possibly below you can do something like this: