Home > Software design >  Problem saving array<object> in backend C# list<object>
Problem saving array<object> in backend C# list<object>

Time:01-15

I have form with grid data table inside form and other fields outside of grid and try to save all this data into the SQL Server DB, but when i try Axios doesn't connect to backend service. I think problem is something about compability of Array with List. I don't know the correct way to do things. Typscript Model is:

import { RepairDetModel } from '../../models/repairDet/repairDetModel';

export class RepairMasterModel {
    public repairMasterId: number;
    public autoUserId: number;
    public year: string;
    public repairDetails: Array<RepairDetModel>;
    public isActive: boolean;

}

Axios request

add(repairMaster: RepairMasterModel) {
        return axios({
            url: `repairMaster/create`,
            method: 'post',
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            data: JSON.stringify(repairMaster)
        })
            .then(res => res.data.data)
            .catch(error =>
                Promise.reject(HelperFunctions.getErrorMessage(error))
            )
    }

And the backend Service

public int Create(RepairMasterDTO repairDTO)
        {
            try
            {
                if (string.IsNullOrEmpty(repairDTO.Year))
                    throw new BusinessValidationException(ErrorCodes.REPAIRMASTER_YEAR_MISSING);
                var repairEntity = new RepairMasterEntity()
                {

                    AutoUserId = repairDTO.AutoUserId,
                    Year = repairDTO.Year,
                    IsActive = repairDTO.IsActive,
                    UpdatedOn = DateTime.Now,
                };
 
                int repairId = DataAccessService.RepairMasterRepository.Create(repairEntity);
                DataAccessService.Commit();

                return repairId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

RepairMasterDTO class

    public class RepairMasterDTO
    {
        public int RepairMasterId { get; set; }
        public string Year { get; set; }
        public int AutoUserId { get; set; }
        public List<RepairDetDTO> RepairDetails { get; set; }
        public bool IsActive { get; set; }
    }

CodePudding user response:

At first glance it looks correct, try adding a [HttpPost] attribute to your endpoint.

CodePudding user response:

Could you show the exact error message? I would first try to use the same type for RepairDetails and RepairDetailsDTO, pick either List or Array. Also make sure the properties on both are the same.

  •  Tags:  
  • Related