我需要验证对象列表并获取有效元素。
当我将验证属性放置到属性字段并发送包含无效字段的对象列表时,所有模型都将变为无效。 Google帮不了我。有没有什么方法可以通过属性来解决此问题,而无需在控制器上进行自定义验证?
public class ConnectionPacketModel
{
[Required(ErrorMessage = "You must specify connections.")]
[MinLength(1, ErrorMessage = "You must specify at least one connection.")]
public List<ConnectionEventModel> Connections { get; set; }
}
public class ConnectionEventModel
{
[Range(1, 65535, ErrorMessage = "Port must be in range [0,65535]")]
public int Port { get; set; }
[Required(ErrorMessage = "You must specify ip address")]
[StringLength(8, MinimumLength = 16, ErrorMessage = "{0} length must be between {2} and {1}.")]
public string Ip { get; set; }
public string Country { get; set; }
public string Protocol { get; set; }
public DateTime CreatedAt { get; set; }
public ConnectionEventModel()
{
CreatedAt = DateTime.Now;
}
}
我使用APIController
和ControllerBase
并认为实现这种行为是可能的