ASP .NET CORE MVC 从具有多个数据的 JSON 中保存信息
我通过 $.ajax() 将 JSON 发送到 mvc 控制器,我想在其中保存给定报告的这些记录。
报告记录保存在带有字段的表中:
id, Reportid, ItemId, CategoryId, UnitCost, Revenue, Units
我只需要更新给定 ID 的单位成本、收入、单位。
JSON 是从 HTML 表收集的来自不同实体的数据的聚合。
我的 JSON(其中 id 是 reportRecords 表中的 id):
[
{"id":"1","category":"xxx","item":"yyy","packing":"zzz","units":"35","unitCost":"45","unitRevenue":"65","unitMargin":"20","marginPercent":"44.44","totCost":"1575","totRevenue":"2275","totMargin":"700,00","marginWeight":"80.65 %"},
{"id":"2","category":"xxx","item":"yyy","packing":"zzz","units":"56","unitCost":"32","unitRevenue":"35","unitMargin":"3","marginPercent":"9.37","totCost":"1792","totRevenue":"1960","totMargin":"168,00","marginWeight":"19.35 %"}
]
我的 C# EF 类:
public class ReportRecord
{
[Key]
public int id { get; set; }
[Required]
public Report Report { get; set; }
[Required]
public Item Item { get; set; }
[Required]
public Category Category{ get; set; }
[Required]
[DataType(DataType.Currency)]
public float? UnitCost { get; set; }
[Required]
[DataType(DataType.Currency)]
public float? Revenue { get; set; }
[Required]
public int Units { get; set; }
}
I send a JSON through $.ajax() to an mvc controller where i'd like to save those records for a given report.
reportRecords are saved in a table with fields:
id, Reportid, ItemId, CategoryId, UnitCost, Revenue, Units
I need to update only UnitCost, Revenue, Units for the given id.
The JSON is an aggregate of data from different entities collected from an HTML table.
My JSON (where id is the id in the reportRecords table):
[
{"id":"1","category":"xxx","item":"yyy","packing":"zzz","units":"35","unitCost":"45","unitRevenue":"65","unitMargin":"20","marginPercent":"44.44","totCost":"1575","totRevenue":"2275","totMargin":"700,00","marginWeight":"80.65 %"},
{"id":"2","category":"xxx","item":"yyy","packing":"zzz","units":"56","unitCost":"32","unitRevenue":"35","unitMargin":"3","marginPercent":"9.37","totCost":"1792","totRevenue":"1960","totMargin":"168,00","marginWeight":"19.35 %"}
]
My C# EF class:
public class ReportRecord
{
[Key]
public int id { get; set; }
[Required]
public Report Report { get; set; }
[Required]
public Item Item { get; set; }
[Required]
public Category Category{ get; set; }
[Required]
[DataType(DataType.Currency)]
public float? UnitCost { get; set; }
[Required]
[DataType(DataType.Currency)]
public float? Revenue { get; set; }
[Required]
public int Units { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是如何解决的:
我必须获取 JSON 数据,选择我需要的数据并保存涉及的实体。我这样解决了:
它有效,但我认为它可能容易产生副作用。
How i solved:
I had to get JSON data, pick the ones i needed and save the involved entities. I solved it this way:
It works but i think it might be prone to side effects.