0

In ASP.NET Core Web API, I have:

public class LoanInfoVM
{
    [Required]
    public string LoanType { get; set; }
    [Required]
    public int LoanAmount { get; set; }
    [Required]
    public int Installments { get; set; }
    public string LoanData { get; set; }
}

EntityMapper:

public class EntityMapper
{
    public LoanInfo FromLoanInfoVMToLoan(LoanInfoVM loan)
    {
        LoanInfo loan = new LoanInfo()
        {
            LoanType = loan.LoanType,
            LoanAmount = loan.LoanAmount,
            Installments = loan.Installments,
        };

        string loanJsonData = JsonSerializer.Serialize(loan);
        return loan;
    }
}

LoanService:

    public async Task<LoanInfo> Post(LoanInfoVM loan)
    {
        var mapper = new EntityMapper();
        var loanDt = mapper.FromLoanInfovmToLoanInfo(loan);

        await _unitOfWork.LoanRepository.Insert(loanDt);
        await _unitOfWork.SaveChangesAsync();

        return loan;
    }

The model consists of LoanType, LoanAmount, Installments, LoanData.

LoanData is JsonSerializer for LoanType, LoanAmount, Installments.

I am only able to insert LoanType,LoanAmount,Installments.

But how do I include LoanData also which is JsonSerializer.Serialize(loan)?

I want to be able to do all these into the same model LoanInfo at the same submit.

Thanks

1
  • Your question is a little hard to follow. Are you saying you want to insert the json representation of a loan into the same row as its other fields? First, why? Second, just insert it as a string field Commented Nov 24, 2021 at 14:41

1 Answer 1

1

In your mapper you could add

public class EntityMapper
{
    public LoanInfo FromLoanInfoVMToLoan(LoanInfoVM loan)
    {
        LoanInfo loan = new LoanInfo()
        {
            LoanType = loan.LoanType,
            LoanAmount = loan.LoanAmount,
            Installments = loan.Installments,
        };
        loan.LoanData = JsonSerializer.Serialize(loan)
        return loan;
    }
}

You have the variable loanJsonData but you never use it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.