Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a DTO Object like that:

public class ImageDto : EntityDTO
{
    public ImageDto()
    {
        Position = new PositionDTO();
        Rotation = new RotationDTO();
        Size = new SizeDTO();
    }

    public string Name { get; set; }
    public string Source { get; set; }
    public string Description { get; set; }
    public string Style { get; set; }
    public string Link { get; set; }
    public int ZIndex { get; set; }
    public string Effect { get; set; }
    public PositionDTO Position { get; set; }
    public RotationDTO Rotation { get; set; }
    public SizeDTO Size { get; set; }

}

And I have a Web Api with a method like that:

    // POST api/Image/CreateImage
    [HttpPost]
    [Route("CreateImage")]
    public IHttpActionResult CreateImage([FromBody]ImageDto imageDto)
    {
        if (imageDto == null)
            return BadRequest();

        return Ok();

    }

When I try to send my DTO from Jquery Ajax, the values of the complex types (PositionDTO, RotationDTO and SizeDTO are always 0.0)

This is my code using jQuery AJAX:

var urlApi = '/api/Image/CreateImage';

    var imageDTO =
    {
        Name: "",
        Source: "",
        Description: "",
        IdSlide: "9aa2d084-dd82-457b-a80d-9af8375c59ff",
        Style: "",
        Link: "",
        ZIndex: "",
        Effect: "",
        SizeDTO: {
            Width: 200,
            Height: 200,
        },
        PositionDTO: {
            PositionLeft: 200,
            PositionTop: 200,
        },

        RotationDTO: { Rotation: 180 },

        IdGenially: "54006b50b59f86143834c187"

    };

    function getData() {
        return $.ajax({
            url: urlApi,
            type: 'POST',
            data: JSON.stringify(imageDTO),
            contentType: "application/json; charset=utf-8"
        });
    }

How can I do for obtain the value of the DTO Object in JavaScript and not a 0.0 for all complex values? The other values are coming fine.

Regards!!

share|improve this question
up vote 1 down vote accepted

Your class does not have *DTO properties, your properties are: Position, Rotation, Size:

public PositionDTO Position { get; set; }
public RotationDTO Rotation { get; set; }
public SizeDTO Size { get; set; }

Try removing DTO from your names:

    Size: {
        Width: 200,
        Height: 200,
    },
    Position: {
        PositionLeft: 200,
        PositionTop: 200,
    },

    Rotation: { Rotation: 180 },
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.