当我尝试验证对象数据注释中的列表不起作用时

时间:2019-07-03 06:27:50

标签: c# .net .net-core data-annotations

尝试在我的api中验证对象,该对象包含另一个对象的列表,我需要验证列表计数必须大于0,并且列表内的属性具有另一个数据注释也需要验证

``

  public class CmsRequestBody
   {

    [JsonProperty("n")]
    public int n { get; set; }

    [JsonProperty("locale")]
    [Required]
    public string Locale { get; set; }
    [JsonProperty("questionList")]
    [Required]
    [EnsureOneElement(ErrorMessage = "At least a person is required")]
    [ValidateEachItemAttribute]

    public List<Question> QuestionList;
}

public class Question
{
    [Required]
    [TwelveDigit]
    [JsonProperty("objectId")]
    public long ObjectId { get; set; }
    [Range(1, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
    [Required]
    [JsonProperty("instanceNumber")]
    public int InstanceNumber { get; set; }
}``   

当我在数据注释自定义代码中添加断点而不输入代码时,这是对象和我的自定义数据注释属性

1 个答案:

答案 0 :(得分:0)

我想添加的答案

{ get; set; } for property

public List<Question> QuestionList