当我们在 ASP.NET API 中发布 JSON 数据时,派生类未映射到模型中

问题描述 投票:0回答:1

我有这种类型的数据,我们有这个

EmployeeBonusDetail
模型类,并且我们在该模型中映射派生类模型列。

public  class EmployeeBonusDetail
{
    public int EmployeeId { get; set; }
    public DateTime Date { get; set; }
    public int PointsEarned { get; set; }
    public string Type { get; set; }
}

public class CertificateBonusDetail : EmployeeBonusDetail
{
    public string CertificateName { get; set; }
}

public class PolicyBonusDetail : EmployeeBonusDetail
{
    public string Category { get; set; }
    public string ToEmail { get; set; }
}

public class CUBenchBonusDetail : EmployeeBonusDetail
{
    public string Course { get; set; }
}

public class CoadingStandardDetails : EmployeeBonusDetail
{
    public string CoadingStandardGrade { get; set; }
}

public class ClientInteractionDetails : EmployeeBonusDetail
{
    public string VerbalCommunication { get; set; }
}

public class DomainKnowledgeBonusDetail : EmployeeBonusDetail
{
    public string DomainName { get; set; }
}

public class EmployeeGradingBonusDetail : EmployeeBonusDetail
{
    public string Communication { get; set; }
    public int CommunicationPoint { get; set; }
    public string TeamCoordination { get; set; }
    public int TeamCoordinationPoint { get; set; }
    public string ProjectPerformance { get; set; }
    public int ProjectPerformancePoint { get; set; }
    public string Ownership { get; set; }
    public int OwnershipPoint { get; set; }
}

public class TraineeUnderTL
{
    public string tl_email { get; set; }
    public List<Trainee> trainees { get; set; }
}

public class Trainee : EmployeeBonusDetail
{
    public int trainee_emp { get; set; }
    public string trainee_name { get; set; }
    public string trainee_email { get; set; }
    public string trainee_skill { get; set; }
    public DateTime tl_assign_date { get; set; }
}

public class EmployeeBonusDetailsRequest
{
    public List<EmployeeBonusDetailsResponse> EmployeeSkillDetails { get; set; }
    public List<EmployeeSkillData> EmployeePointData { get; set; }
}

public class EmployeeBonusDetailsResponse
{
    public string CategoryTable { get; set; }
    public List<EmployeeBonusDetail> Data { get; set; }
}

但是当我们在此方法中发布数据时,派生类列不会被映射:

[Authorize]
[HttpPost(nameof(SaveGeneratedSkillBonusDetails))]
public async Task<IActionResult> SaveGeneratedSkillBonusDetails([FromBody] EmployeeBonusDetailsRequest request)
{
    if (request == null || request.EmployeeSkillDetails == null || request.EmployeePointData == null)
    {
        return BadRequest("Invalid data received.");
    }

    try
    {
        var employeeSkillDetails = request.EmployeeSkillDetails;
        var employeePointData = request.EmployeePointData;

        // Save logic goes here

        return Ok(true);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex.Message);
        return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
    }
}

提供解决此问题的解决方案,因为当我们发布此模型 JSON 数据时,会显示所有数据,但派生类列未映射。

c# asp.net-mvc asp.net-core
1个回答
0
投票

您可以使用包来帮助您实现派生类的绑定。您可以从 nueget 包安装它,并将以下代码添加到 program.cs 中:

dotnet 添加包 JsonSubTypes

并使用NewtonsoftJson

dotnet 添加包 Microsoft.AspNetCore.Mvc.NewtonsoftJson

修改program.cs:

builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.Converters.Add(
        JsonSubtypesConverterBuilder
            .Of(typeof(EmployeeBonusDetail), "Type") // Specify discriminator property
            .RegisterSubtype(typeof(CertificateBonusDetail), "Certificate")
            .RegisterSubtype(typeof(PolicyBonusDetail), "Policy")
            .RegisterSubtype(typeof(CUBenchBonusDetail), "Bench")
            .RegisterSubtype(typeof(CoadingStandardDetails), "CodingStandard")
            .RegisterSubtype(typeof(ClientInteractionDetails), "ClientInteraction")
            .RegisterSubtype(typeof(DomainKnowledgeBonusDetail), "DomainKnowledge")
            .RegisterSubtype(typeof(EmployeeGradingBonusDetail), "Grading")
            .Build()
    );
});

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.