ASP .NET CORE MVC 从具有多个数据的 JSON 中保存信息

发布于 2025-01-10 13:03:26 字数 1346 浏览 0 评论 0原文

我通过 $.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-01-17 13:03:26

我是如何解决的:

我必须获取 JSON 数据,选择我需要的数据并保存涉及的实体。我这样解决了:

// POST: Reports/Asave/5
[HttpPost]
public async Task<IActionResult> Asave(int? id)
{
    var json = Request.Query["json"][0];

    var jsonAsArray = JArray.Parse(json);

    float reportTotalCost = 0;
    float reportTotalRevenue = 0;

    foreach (var item in jsonAsArray)
    {
        reportTotalRevenue += item.Value<float>("totRevenue");
        reportTotalCost += item.Value<float>("totCost");

        var reportRecord = _context.ReportRecords.Find(item.Value<int>("id"));
        reportRecord.Revenue = item.Value<float>("unitRevenue");

        _context.SaveChanges();
    }

    var report = _context.Reports.Find(id);

    report.Revenue = reportTotalRevenue;
    
    _context.SaveChanges();

    return Json(new { code = 200, result = "success" });
}

它有效,但我认为它可能容易产生副作用。

How i solved:

I had to get JSON data, pick the ones i needed and save the involved entities. I solved it this way:

// POST: Reports/Asave/5
[HttpPost]
public async Task<IActionResult> Asave(int? id)
{
    var json = Request.Query["json"][0];

    var jsonAsArray = JArray.Parse(json);

    float reportTotalCost = 0;
    float reportTotalRevenue = 0;

    foreach (var item in jsonAsArray)
    {
        reportTotalRevenue += item.Value<float>("totRevenue");
        reportTotalCost += item.Value<float>("totCost");

        var reportRecord = _context.ReportRecords.Find(item.Value<int>("id"));
        reportRecord.Revenue = item.Value<float>("unitRevenue");

        _context.SaveChanges();
    }

    var report = _context.Reports.Find(id);

    report.Revenue = reportTotalRevenue;
    
    _context.SaveChanges();

    return Json(new { code = 200, result = "success" });
}

It works but i think it might be prone to side effects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文