亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Postman 在 Asp.net Core API 中發布數據時將值轉換為類型時出錯

通過 Postman 在 Asp.net Core API 中發布數據時將值轉換為類型時出錯

C#
浮云間 2022-10-15 15:24:27
我正在 Asp.net Core 中開發一個 API。我有聯系人和職位模型。聯系人.cspublic 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}職稱.cspublic 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; }}我知道問題是 API 需要參考中的 jobtitle 對象,但我不知道如何給出它,因為它沒有達到我在 Visual Studio 中的后斷點。那么如何獲取 jobtitle 的值并進行轉換呢?
查看完整描述

2 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

您可以發送一個只有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": "[email protected]",

  "phone": 12345678,

  "mobile": 12345678,

  "address": "84445 abc Hill",

  "photo": "",

  "isDeleted": false,

  "createdOn": "2018-03-07T03:41:44",

  "updatedOn": "2018-03-07T03:41:44"

}


查看完整回答
反對 回復 2022-10-15
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

你可以這樣做:


-edit:您在評論中說,我更新了答案。如果您不想使用 JSON 發送 JobTitle,只需刪除該行。它將 NULL 映射為您的 JobTitle 對象。所以你可以在后端代碼中處理它。


{

"firstName": "XYZ",

"middleName": "",

"lastName": "ABS",

"fullName": "XYZ ABS",

"gender": 0,

"dateOfBirth": "1987-03-05T07:49:33",

"maritalStatus": 1,

"email": "[email protected]",

"phone": 12345678,

"mobile": 12345678,

"address": "84445 abc Hill",

"photo": "",

"isDeleted": false,

"createdOn": "2018-03-07T03:41:44",

"updatedOn": "2018-03-07T03:41:44"

}


查看完整回答
反對 回復 2022-10-15
  • 2 回答
  • 0 關注
  • 150 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號