【问题标题】:ASP.NET MVC Data annotations for json key用于 json 键的 ASP.NET MVC 数据注释
【发布时间】:2015-05-29 10:16:36
【问题描述】:

我在 ASP.Net MVC 中有一个类似这样的模型。

public class DataTable
        {
            public int RecordsTotal { get; set; }

            public int RecordsFiltered { get; set; }

            public int Draw { get; set; }

        }

是否可以通过在每个属性中添加数据注释将其输出为 JSON? 输出应该是这样的。

{
recordsTotal:10,
recordsFiltered:10,
draw:1 
}

谢谢!

【问题讨论】:

  • 您想添加哪些数据注释,究竟是什么?
  • RecordsTotal 转换为键recordsTotal 的数据注释。将 json 返回到客户端/浏览器时。
  • 这里唯一的区别是大写吗?否则,默认情况下会返回一个 JSON 对象:{ RecordsTotal:10, RecordsFiltered:10, Draw:1 }
  • 是的,我唯一想要的就是将它作为 CamelCase 返回。

标签: json asp.net-mvc


【解决方案1】:

除了开头的大写字母外,您可以返回一个与您发布的对象一模一样的对象:

return Json(new DataTable() {
    RecordsTotal = 10,
    RecordsFiltered = 10,
    Draw = 1
});

如果您真的需要一个小写的属性名称then thereexisting questionsanswer this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2012-04-11
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多