简体   繁体   中英

Problem saving array<object> in backend C# list<object>

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; }
    }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM