如何从控制器向Json响应动态添加更多属性 [英] How to add dynamically more properties to Json response from the controller

查看:111
本文介绍了如何从控制器向Json响应动态添加更多属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象,我将其包装在Json中,从控制器返回到客户端.

I have an object which I return wrapped in Json from the controller to the client.

return Json(result); //result is DataSourceResult

我想在此Json响应中添加更多属性.

I would like to add some more properties inside this Json response.

我该怎么办?

我尝试使用Json属性Data,还将DataSourceResult强制转换为对象并尝试对其进行扩展,但是我所拥有的一切似乎都不是一个好的解决方案.

I tried using Json property Data and also casting the DataSourceResult to object and trying to expand it, but whatever I have doesn't look like a good solution.

推荐答案

您可以像这样返回anonymous object

return Json(new {
    result = result,
    myProperty1 = value1,
    myProperty2 = value2
});

这篇关于如何从控制器向Json响应动态添加更多属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆