0

The post returns status code 200, but the values are never getting passed to the controller. My DTO originally consisted of two objects a reservation and a list of virtual computers. I tried taking the attributes out of the reservation and putting them directly into the DTO to see if any of them wasn't being converted correctly with no luck. If I change the endpoint parameter to just take the list of virtual pcs or the reservation, it will post it correctly with just that object, but won't take the DTO containing both objects.

My HTTPClient for making the post:

public async Task<bool> CreateReservation(ReservationDTO reservation, List<VirtualPCDTO> virtualPCs)
        {
            string result = string.Empty;
            CreateReservationDTO createReservationDTO = new CreateReservationDTO
            {
                ReservationId = reservation.Id,
                ReservationFromDate = reservation.FromDate.Value,
                ReservationToDate = reservation.ToDate.Value,
                ReservationTimeStamp = reservation.Timestamp.Value,
                ReservationIp = reservation.Ip,
                VirtualPCs = virtualPCs
            };

            try
                {
                    var response = await client.PostAsJsonAsync(baseAddress + "api/Scheduler/CreateReservation", createReservationDTO);
                    result = await response.Content.ReadAsStringAsync();
                }
                catch (Exception e)
                {
                    if (e is ArgumentNullException)
                    {
                        throw new ArgumentNullException(e.Message, e.InnerException);
                    }
                    else if (e is HttpRequestException)
                    {
                        throw new HttpRequestException(e.Message, e.InnerException);
                    }
                }

            return result.Equals("true");
        }
    }

My controller:

        [Route("CreateReservation")]
        [ProducesResponseType(200, Type = typeof(bool))]
        [ProducesResponseType(400, Type = typeof(bool))]
        public async Task<IActionResult> CreateReservationJobAsync([FromBody]CreateReservationDTO data)
        {
            SchedulerRepository schedulerRepository = new SchedulerRepository(_firewallSettings);

            try
            {
                //will add some jobs to a Quartz scheduler based on the reservation and the virtual pcs
            }
            catch (Exception)
            {
                return BadRequest(false);
            }

            return Ok(true);
        }

I have two breakpoints, first to see the data I send, where I can see I instantiate the DTO correctly. The second breakpoint is in the controller, where I can see all the values from the parameter is either NULL or default values. My DTO for the POST:

public class CreateReservationDTO
    {
        public int ReservationId;
        public DateTime ReservationFromDate;
        public DateTime ReservationToDate;
        public DateTime ReservationTimeStamp;
        public string ReservationIp;

        public List<VirtualPCDTO> VirtualPCs;
    }

VirtualPCDTO:

public class VirtualPCDTO : ResourceDTO
    {
        public OpnSenseDTO OpnSense { get; set; }
        public string Name { get; set; }
        public string InternalHostname { get; set; }
        public string InternalIp { get; set; }
        public string InternalPort { get; set; }
        public string ExternalHostname { get; set; }
        public string ExternalIp { get; set; }
        public string ExternalPort { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public int? ResolutionWidth { get; set; }
        public int? ResolutionHeight { get; set; }
        public int ResourceId { get; set; }
        public int? ParentId { get; set; }
        public bool? IsSubComputer { get; set; }
    }

ResourceDTO:

public class ResourceDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsAvailable { get; set; }
        public DateTime? Timestamp { get; set; }
        public string RowVersion { get; set; }
    }

OpnSenceDTO:

public class OpnSenseDTO
    {
        public int Id { get; set; }
        public bool IsEnabled { get; set; }
        public string Alias { get; set; }
        public string UUID { get; set; }
        public string RowVersion { get; set; }
    }
Guffe
  • 1
  • 1
  • can you post the view where youre posting the data please and/or the script if it's posted via javascript/ajax – GregH Jun 29 '21 at 13:33
  • 2
    Have you noticed that `CreateReservationDTO` is missing getters/setters? – Roar S. Jun 29 '21 at 13:40
  • @GregH the request is coming from the HTTPClient `await client.PostAsJsonAsync(baseAddress + "api/Scheduler/CreateReservation", createReservationDTO);` – phuzi Jun 29 '21 at 13:41
  • 1
    I think @RoarS. has nailed it. Model binding only works on properties not fields. – phuzi Jun 29 '21 at 13:45
  • 1
    @RoarS. Thanks it was what I was missing! – Guffe Jun 29 '21 at 13:54

0 Answers0