使用充血模型构建类时,Web API无法获取前端传过来的参数

问题:构建充血模型Item类,在前端明明传入了Amount对象参数,后端却无法获取

 以下是Item类中的Amount属性及构造函数

 1    public Amount? Amount { get;private set; }
 2    private Item()
 3    {
 4 
 5    }
 6    public Item(string name)
 7    {
 8       this.Name = name;
 9       this.Id=Guid.NewGuid();
10       this.CreateTime=DateTime.Now;
11    }

解决方案:

应该在构造函数时对Amount属性进行初始化才能在控制器中接收

public Item(string name,Amount? amount,string? Des,Guid? sordId,DateTime? expDate)
{
    this.Name = name;
    this.Id=Guid.NewGuid();
    this.CreateTime=DateTime.Now;
    this.Amount = amount;
    this.Description = Des;
    this.SortId = sordId;
    this.ExpirationDate = expDate;
}

 

posted @ 2023-02-19 22:54  守护月亮的星星  阅读(29)  评论(0)    收藏  举报