0
我试图在提交给服务器之前强制一个字段。为此,我使用[必需的]数据注释进行模型验证。它如预期的那样工作,字符串数据类型但不是双。在json中找不到所需的属性
由于某种原因,它不适用于double type属性。 下面是我对模式代码:
public class ProductMetadata
{
[Required]
public string Barcode { get; set; }
[StringLength(50)]
[Required(AllowEmptyStrings = false, ErrorMessage="Please insert the product name!")]
public string Name { get; set; }
[Range(0, 5000)]
public double ShippingCostPerUnit { get; set; }
[Range(0, 10000)]
public int QuantityForFreeShipping { get; set; }
public Nullable<int> CategoryId { get; set; }
public bool IsDeleted { get; set; }
[Range(0, 1000000)]
[Required(ErrorMessage="Please provide a unit price for the product!")]
public double UnitPrice { get; set; }
}
的响应体是一个JSON响应并没有完成必填字段中有以下内容:
{
"Message":"The request is invalid.",
"ModelState":
{"product":["Required property 'UnitPrice' not found in JSON. Path '', line 1, position 33."],
"product.Barcode":["The Barcode field is required."],
"product.Name":["Please insert the product name!"]
}
}
我不不明白为什么工作正常名称和条码而不是单价。
编辑1:
如果我删除[必需]属性,我把其输入为单价 -1我收到相应的验证消息,那么为什么不工作的要求属性?
编辑2:请求有效负载(也更新了ProductMetadata类):
{IsDelete: false, CategoryId: 1}
CategoryId: 1
IsDelete: false
任何帮助表示赞赏!谢谢!
你能分享你的请求有效载荷 - JSON吗? – Paritosh
@Paritosh我编辑了这个问题 – sixfeet