locked
Json File return Column Null .. Why ? RRS feed

  • Question

  • User1670960895 posted

    First I Create this API Controller ... and Implement This Function :

    public IHttpActionResult GetCustomer()
    {
    var customerDto = _context.Customer
    .Include(c=>c.MemberShip)
    .ToList()
    .Select(Mapper.Map<Customer,CustomerDto>);
    return Ok(customerDto);
    }

    Then Create this Class:

    public class MemberShipDto
    {
    public byte Id { get; set; }
    public string MemShipName { get; set; }
    }

    Then Add this Attribute:

    public MemberShipDto MemberShipType { get; set; }

    Than Add This in Mapper Profile:

    CreateMap<MemberShip, MemberShipDto>();

    And When I Test this Function the JSON result is the Following here:

    {
            "id": 6,
            "cutName": "Ahmed Elbadry",
            "birthdate": "2018-01-04T00:00:00",
            "memberShipID": 3,
            "memberShipType": null,
            "isSubscriped": true
        }

    My Question Here Why "memberShipType" is null ??

    Sunday, January 14, 2018 2:39 AM

All replies

  • User1564875471 posted

    Check the automapper mappings. I see the original name of the property is "Membership" while it's name in the DTO is "memberShipType". Either use same name or create a mapping to this property. You might need a custom automapper mapping rule like this:

    Mapper.CreateMap<MemberShip, MemberShipDto >() .ForMember(dest => dest.memberShipType, opt => opt.MapFrom(src => src.MemberShip));

    Sunday, January 14, 2018 1:40 PM