【发布时间】:2018-06-07 07:27:26
【问题描述】:
客户端模型(类型脚本文件):
export interface IRecord {
id: string
amount: string,
amountConst: string,
amountLC: string,
}
服务器端模型/类:我正在使用 newtonsoft json。
如果我将 Json 属性值更改为 typescript 文件中的属性名称,那么 该列值在 UI 上不可见。
public class Records
{
[Key]
public string ID { get; set; }
[Column("Amount")]
[JsonProperty("Amount")]
public string Amount { get; set; }
[Column("Amount Const $")]
[JsonProperty("Amount Const $")]
public string AmountConst { get; set; }
[Column("Amount LC")]
[JsonProperty("Amount LC")]
public string AmountLC { get; set; }
}
Web API 控制器:
[HttpPost]
[Route("Export")]
[ActionName("Export")]
public FileResult Export([FromBody]List<Records> Record)
{
try
{
}
}
服务器端我正在从客户端获取准确的记录数 但具有 null 属性值。
【问题讨论】:
-
您应该在任何地方删除
JsonProperty并以角度修复您的绑定以使用没有空格的版本。当您添加带有空格的JsonProperty时,ASP.NET Core 需要像[{ "Amount": 5, "Amount Const $": "abc", "Amount LC": "xyz" }]这样的 json 主体,但您的 Angular 客户端会发送[{ "amount": 5, "amountConst": "abc", "amountLC": "xyz" }] -
@Tseng 我正在使用合同解析器将属性名称转换为 Typescript 文件中的名称,将 Pascalcase 转换为 camelcase
标签: c# json asp.net-core .net-core asp.net-core-mvc