通过 Postman 在 Asp.net Core API 中发布数据时将值转换为类型时出错

Error converting value to type while posting data in Asp.net Core API through Postman

我正在 Asp.net Core 中开发 API。我有联系人和职位模型。

Contact.cs

public class Contact
{
    [Key]
    public int ContactId { get; set; }

    //public string UserId { get; set; }

    [Required]
    [StringLength(20)]
    public string FirstName { get; set; }

    [StringLength(20)]
    public string MiddleName { get; set; }

    [Required]
    [StringLength(20)]
    public string LastName { get; set; }

    [StringLength(60)]
    public string FullName { get; set; }

    [Required]
    public Gender Gender { get; set; }

    [Required]
    public DateTime DateOfBirth { get; set; }

    public MaritalStatus MaritalStatus { get; set; }

    [Required]
    public JobTitle JobTitle { get; set; }

    [Required]
    [EmailAddress]
    [StringLength(50)]
    public string Email { get; set; }

    public int Phone { get; set; }

    [Required]
    public int Mobile { get; set; }

    public string Address { get; set; }
    public string Photo { get; set; }
    public bool IsDeleted { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

public enum Gender
{
    Male = 0,
    Female = 1
}

public enum MaritalStatus
{
    Unmarried = 0,
    Married = 1,
    Divorced = 2,
    Widowed = 3
}

Jobtitle.cs

public class JobTitle
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Required]
    [StringLength(50)]
    public string NameAr { get; set; }

    public bool IsDeleted { get; set; }
    public DateTime? CreatedOn { get; set; }
    public DateTime? UpdatedOn { get; set; }
    public virtual ICollection<Contact> Contact { get; set; }
}

我正在使用 Postman post 我的 JSON 数据在联系人中插入一个新值,但它给我错误:

JSON 值

{
"firstName": "XYZ",
"middleName": "",
"lastName": "ABS",
"fullName": "XYZ ABS",
"gender": 0,
"dateOfBirth": "1987-03-05T07:49:33",
"maritalStatus": 1,
"jobTitle": "1",
"email": "demo@demo.com",
"phone": 12345678,
"mobile": 12345678,
"address": "84445 abc Hill",
"photo": "",
"isDeleted": false,
"createdOn": "2018-03-07T03:41:44",
"updatedOn": "2018-03-07T03:41:44"
}

将值转换为类型 Jobtitle 对象时出现错误。

"errors": {
"jobTitle": [
  "Error converting value \"1\" to type 'TransConnectApi.Models.JobTitle'. Path 'jobTitle', line 9, position 17."
]
}

我知道问题是 API 需要参考中的职位对象,但我不知道如何给出它,因为它没有达到我在 [=39= 中的 post 断点].那么如何获取jobtitle的值并进行转换呢?

你可以这样做:

-edit:你在评论里说了,我更新了答案。如果您不想发送带有 JSON 的 JobTitle,只需删除该行。它将 NULL 映射为您的 JobTitle 对象。所以你可以在后端代码中处理它。

{
"firstName": "XYZ",
"middleName": "",
"lastName": "ABS",
"fullName": "XYZ ABS",
"gender": 0,
"dateOfBirth": "1987-03-05T07:49:33",
"maritalStatus": 1,
"email": "demo@demo.com",
"phone": 12345678,
"mobile": 12345678,
"address": "84445 abc Hill",
"photo": "",
"isDeleted": false,
"createdOn": "2018-03-07T03:41:44",
"updatedOn": "2018-03-07T03:41:44"
}

您可以发送一个只有 Id 键的对象,例如:

{
  "firstName": "XYZ",
  "middleName": "",
  "lastName": "ABS",
  "fullName": "XYZ ABS",
  "gender": 0,
  "dateOfBirth": "1987-03-05T07:49:33",
  "maritalStatus": 1,
  "jobTitle": {  // here just pass object with Id key
    "Id": "1"
  },
  "email": "demo@demo.com",
  "phone": 12345678,
  "mobile": 12345678,
  "address": "84445 abc Hill",
  "photo": "",
  "isDeleted": false,
  "createdOn": "2018-03-07T03:41:44",
  "updatedOn": "2018-03-07T03:41:44"
}