ASP.NET核心数据无法从请求主体绑定到dto

时间:2017-04-03 13:10:36

标签: asp.net-core

ASP.NET核心中的模型绑定存在问题,我正在使用" Microsoft.AspNetCore.Mvc":" 1.0.1"

这是我的api方法

[Route("attach/{buildingId}")]
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> AttachToBuilding([FromRoute]Guid buildingId,[FromBody] AttachmentDto dto)

AttachmentDto dto始终等于null

这是dto

public class AttachmentDto
    {
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Phone { get; set; }
        [Required]
        public string UniqueDeviceId { get; set; }
        [Required]
        public int ApartmentNumber { get; set; }
        [Required]
        public int AgreementNumber { get; set; }
        [Required]
        public int FloorNumber { get; set; }
    }

这是我在正文请求中发送给api的数据

  {
      "firstName": "Artem",
      "lastName": "Lys",
      "middleName": "Викторович",
      "phone": "93840958093",
      "uniqueDeviceId": "879987",
      "apartmentNumber": "2",
      "agreementNumber": "3аываыва4545",
      "floorNumber": "5"
    }

1 个答案:

答案 0 :(得分:0)

我在使用asp.net核心传递参数时遇到了很多问题,我已经设法解决了这个问题。

    [HttpPost]
    public string Atualizar([Bind("Id,Descricao,Conclusao")] Exame exame)
    {
        ...
    }

请求

$http({
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        method: "POST",
        url: URL_API + 'Exames/Atualizar,
        data: $.param(exame),
    }).then(function(response) {
        ...
    });

这项工作对我而言。

在我的案例中工作的秘诀是将json对象转换为 $ .param(exame)