ASP.NET核心-错误CS1503参数1:无法在Excel上载中从“Request.CreateMandateDto”转换为“Response.MandateListDto”

ipakzgxi  于 2023-06-07  发布在  .NET
关注(0)|答案(1)|浏览(356)

我上传Excel文件使用ASP.NET Core-6 Web API与EPPLUS包。
我有两个DTO:CreateMandateDto作为请求DTO,MandateListDto作为响应DTO
我的代码如下所示:

回复:

public class Response<T>
{
    public T Data { get; set; }
    public bool Successful { get; set; }
    public string Message { get; set; }
    public int StatusCode { get; set; }

    public Response(int statusCode, bool success, string msg, T data)
    {
        Data = data;
        Successful = success;
        StatusCode = statusCode;
        Message = msg;
    }

    public Response()
    {
    }

    public static Response<T> Fail(string errorMessage, int statusCode = 404)
    {
        return new Response<T> { Successful = false, Message = errorMessage, StatusCode = statusCode };
    }

    public static Response<T> Success(string successMessage, T data, int statusCode = 200)
    {
        return new Response<T> { Successful = true, Message = successMessage, Data = data, StatusCode = statusCode };
    }

    public override string ToString() => JsonConvert.SerializeObject(this);
}

ExcelHelper:

public static class ExcelHelper
{
    public static List<CreateMandateDto> ReadMandatesFromExcel(IFormFile excelFile)
    {
        List<CreateMandateDto> mandates = new List<CreateMandateDto>();

        using (var package = new ExcelPackage(excelFile.OpenReadStream()))
        {
            var worksheet = package.Workbook.Worksheets[0]; 
            int rowCount = worksheet.Dimension.Rows;

            for (int row = 2; row <= rowCount; row++) 
            {
                var mandate = new CreateMandateDto();

                mandate.DrAccountNumber = worksheet.Cells[row, 1].Value.ToString();
                mandate.StartDate = DateTime.Parse(worksheet.Cells[row, 2].Value.ToString());
                mandate.EndDate = DateTime.Parse(worksheet.Cells[row, 3].Value.ToString());
                mandate.PaymentFrequency = (PaymentFrequency)Enum.Parse(typeof(PaymentFrequency), worksheet.Cells[row, 4].Value.ToString());
                mandate.Amount = decimal.Parse(worksheet.Cells[row, 5].Value.ToString());

                mandates.Add(mandate);
            }
        }

        return mandates;
    }
}

Excel助手将在Excel工作表经过验证并传递到数据库之前读取它。

**AddMandateAsyncEngine:**将数据插入Database。

public async Task<Response<MandateListDto>> AddMandateAsyncEngine(CreateMandateDto requestDto)
    {
        var response = new Response<MandateListDto>();

            var mandate = _mapper.Map<Mandate>(requestDto);
            string transactionJsonData = JsonConvert.SerializeObject(mandate);

            mandate.CreatedBy = userName;
            mandate.RawData = transactionJsonData;
            mandate.PaymentCount = (int)numberOfTimes;
            mandate.CreatedBy = userName;
            await _unitOfWork.MerchantMandates.InsertAsync(mandate);
            await _unitOfWork.Save();
        }
        return response;
    }

**UploadMerchantMandateExcelAsync:**这将整理并实现所有内容。它结合了这两种方法。验证,插入成功的数据并显示,然后将不允许失败的,但显示不成功的

public async Task<Response<List<MandateListDto>>> UploadMerchantMandateExcelAsync(IFormFile excelFile)
{
    var mandates = ExcelHelper.ReadMandatesFromExcel(excelFile);
    var response = new Response<List<MandateListDto>>();

    if (excelFile == null || excelFile.Length <= 0)
    {
        response.StatusCode = (int)HttpStatusCode.OK;
        response.Message = "No File Available for Upload";
        response.Successful = false;
        return await Task.FromResult(response);
    }

    var successfulMandates = new List<MandateListDto>();
    var failedMandates = new List<MandateListDto>();

    foreach (var mandate in mandates)
    {
        var result = await AddMandateAsyncEngine(mandate);

        if (result.Successful)
        {
            successfulMandates.Add(result.Data);
        }
        else
        {
            failedMandates.Add(mandate);
        }
    }

    if (successfulMandates.Any())
    {
        response.Data = successfulMandates;
        response.Successful = true;
        response.StatusCode = (int)HttpStatusCode.OK;
        response.Message = "Mandates created successfully.";
    }
    else
    {
        response.Data = failedMandates;
        response.Successful = false;
        response.StatusCode = (int)HttpStatusCode.BadRequest;
        response.Message = "Failed to create mandates.";
    }

    return response;
}

但是,我得到了这个错误:
严重性代码说明项目文件行抑制状态错误CS1503参数1:无法从“Request.CreateMandateDto”转换为“Response.MandateListDto”
然后mandate在failedMandates中突出显示。添加(mandate)
我该如何解决这个问题?

xqkwcwgp

xqkwcwgp1#

你的方法从助手ReadMandatesFromExcel返回CreateMandateDto的列表:

List<CreateMandateDto> ReadMandatesFromExcel(IFormFile excelFile)

您创建的用于捕获失败对象的列表的类型为MandateListDto:

var failedMandates = new List<MandateListDto>();

因此,将类型为CreateMandateDto的对象添加到类型为MandateListDto的列表在这里将不起作用:

else
{
  failedMandates.Add(mandate);
}

相关问题