json 如何创建一个对象与 Postman ?

bq3bfh9z  于 2023-01-06  发布在  Postman
关注(0)|答案(1)|浏览(151)

如何创建一个服装对象使用 Postman ?当我尝试这样做 Postman 说,字段设计师_服装和时装之家是必需的如何解决它?
我的模特:
衣服:

public class Clothes:IEntityBase
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
        public string ImageUrl { get; set; }
        public ClothesCategory ClothesCategory { get; set; }

        public List<Designer_Clothes> Designer_Clothes { get; set; }

        public int FashionHouseId { get; set; }
        [ForeignKey("FashionHouseId")]
        public FashionHouse FashionHouse { get; set; }
    }

设计者:

public class Designer:IEntityBase
    {
        [HiddenInput]
        [Key]
        public int Id { get; set; }
        [Display(Name = "Profile Picture")]
        [Required(ErrorMessage ="Profile picture is required")]
        public string ProfilePictureUrl { get; set; }
        [Display(Name = "Full Name")]
        [Required(ErrorMessage ="Full name is required")]
        [StringLength(25, MinimumLength =3,ErrorMessage ="Full name must be between 3 and 25 chars")]
        public string FullName { get; set; }
        [Display(Name = "Bio")]
        [Required(ErrorMessage ="Biography is required")]
        public string Bio { get; set; }
        public List<Designer_Clothes>? Designer_Clothes { get; set; }
    }

设计师_服装

public class Designer_Clothes
    {
        public int ClothesId { get; set; }
        public Clothes Clothes { get; set; }
        public int DesignerId { get; set; }
        public Designer Designer { get; set; }
    }

时装屋

public class FashionHouse:IEntityBase
    {
        [Key]
        public int Id { get; set; }
        [Display(Name ="Profile picture")]
        [Required(ErrorMessage ="Profile picture is required")]
        public string ProfilePictureUrl { get; set; }
        [Display(Name ="Full Name")]
        [StringLength(50,MinimumLength =3,ErrorMessage ="Full name must be between 3 and 50 chars")]
        [Required(ErrorMessage = "Full name is required")]
        public string FullName { get; set; }
        [Display(Name = "Bio")]
        [Required(ErrorMessage = "Biography is required")]
        public string Bio { get; set; }

        public List<Clothes>? Clothes { get; set; }
    }

这是控制器中的Post方法

[HttpPost]
        public async Task<IActionResult> Create([FromBody] Clothes clothes)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            await _service.AddAsync(clothes);
            return Created($"/api/book/{clothes.Id}", clothes);
        }

我尝试后json像这样

{
        "name": "Second item ",
        "description": "Description of second item test",
        "price": 50,
        "imageUrl": "sdfdsfsdfsf",
        "clothesCategory": 3,
        "designer_Clothes": [{
            "ClothesId":1,
            "DesignerId":2
            
        }],
        "fashionHouseId": 2,
        "fashionHouse": {
            "Bio":"TEST",
            "FullName":"TESTE",
            "ProfilePictureUrl":"ASdASd"
        }
    }

但它不起作用,有人知道为什么吗

2j4z5cfb

2j4z5cfb1#

Designer_Clothes具有必需的属性Clothes(不能为空),但json缺少以下数据:

{
    ...
    "name": "Second item ",
    "designer_Clothes": [{
        "ClothesId":1,
        "DesignerId":2
        // Miss > "Clothes": { ... }
    }],
    ...
}

但是clothe也是根对象,这是一个引用循环:

{
    ...
    "name": "Second item ",
    "designer_Clothes": [{
        "ClothesId":1,
        "DesignerId":2
        "Clothes": { 
            ...
            "name": "Second item ",
            "designer_Clothes": [{
                "ClothesId":1,
                "DesignerId":2
                "Clothes": { REFERENCE LOOP }
            }],
            ...
        }
    }],
    ...
}

可以调整工作的模型......但由于API模型也是实体模型,这也会影响DB模型。
也许你可以考虑把API的模型和DB的模型分开,然后你可以声明一个类,比如:

namespace MyApi.Model;

public class ClotheCreating
{
    public string Name { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public string ImageUrl { get; set; }
    public ClothesCategory ClothesCategory { get; set; }
    public List<int> DesignerIds { get; set; }
    public int FashionHouseId { get; set; }
}

相关问题