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

沙伯·伊斯梅尔

我正在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; }
}

我正在使用邮递员发布我的JSON数据以在联系人中插入新值,但它给了我错误:

JSON值

{
"firstName": "XYZ",
"middleName": "",
"lastName": "ABS",
"fullName": "XYZ ABS",
"gender": 0,
"dateOfBirth": "1987-03-05T07:49:33",
"maritalStatus": 1,
"jobTitle": "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"
}

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

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

我知道问题在于API需要引用jobtitle对象,但是我不知道该如何解决,因为它没有达到Visual Studio中的发布断点。那么,如何才能获得Jobtitle的价值并将其转换呢?

Prashant皮姆帕莱

您可以发送仅带有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"
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ASP.NET Core 通过 Postman 调用 API

通过 Postman 将文件上传到 ASP.NET Core API 时出现错误 415

将带Postman的PATCH发送到Asp.net Core webapi时出错

将MYSQL与ASP.NET Core连接时出错

在ASP.NET Core中发布复杂类型

从asp net 5迁移到asp net core时出错

将数据发布到控制器 Asp.Net Core MVC 时模型为空

如何使用 Postman 将 JSON 数组发布到 ASP.Net Core 应用程序

在ASP.NET Core Web Api中发布流

ASP.NET Core通过ID获取数据列表

ASP.NET MVC Core - 通过 HttpClient 使用 API

邮递员中的输入数据无效!!Postman 测试 API asp.net core

从Visual Studio 2019将ASP.NET Core 3.1网站发布到Azure时出错

通过AJAX将JSON对象发布到ASP.NET Core Web API

配置服务时出错 - 将 Entity Framework Core 添加到 ASP.NET Core

如何将PATCH与ASP.NET Core和Postman一起使用?

如何使用Postman使用Cookie身份验证测试ASP.NET Core Web API?

Postman JSON 不适用于 ASP.NET Core Web API

从发布到2.2的ASP.Net Core解决方案在发布时将无法运行

ASP.NET Core 发布时的预编译视图

ASP.NET Core:在发布时排除或包含文件

将CKEDITOR值发布到Asp.net Core中

创建 callbackUrl asp.net core 时出错

ASP.NET Core-尝试使用HealthChecks时出错

在ASP.NET Core中插入/更新记录时出错

ASP.NET Core 2.1-实现MemoryCache时出错

从 Ionic 访问 ASP.NET Core WebApi 时出错

MVC ASP.NET Core 中的验证字段时出错

ASP.NET Core Web API - 无法将类型“System.Threading.Tasks.Task<Merchant?>”隐式转换为“Merchant”