我正在asp.net上使用Code First,并且具有以下课程:
public class Producto
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ProductoId { get; set; }
[Remote(action: "VerifySku", controller: "Producto", ErrorMessage = "El sku ya está asignado a un producto")]
[DisplayName("SKU")]
public string Sku { get; set; }
[Required(ErrorMessage = "Seleccione categoría")]
[DisplayName("Categoria")]
public Guid SubCategoriaProductoId { get; set; }
[DisplayName("Categoria")]
public SubCategoriaProducto SubCategoriaProducto { get; set; }
public string Marca { get; set; }
[Required(ErrorMessage = "Ingrese nombre")]
[Remote(action: "VerifyName", controller: "Producto", ErrorMessage = "El producto ya existe")]
public string Nombre { get; set; }
public string Detalle { get; set; }
[Required(ErrorMessage = "Ingrese precio")]
public int Precio { get; set; }
public bool PorEncargo { get; set; }
[Remote(action: "VerifyStock", controller: "Producto", AdditionalFields = nameof(PorEncargo))]
public int Stock { get; set; }
[DefaultValue(1)]
public int EstaActivo { get; set; }
public virtual ICollection<ImagenProducto> Imagenes { get; set; }
}
我需要添加自定义验证,只要PorEncargo字段为false,您就必须需要库存。我没有在ASP.Net Core中找到任何操作方法。
我需要添加自定义验证,在此验证中,只要PorEncargo字段为false,就必须需要库存。
public int Stock { get; set; }
从this doc,您可能会发现。NET Core 3.0和更高版本中的验证系统将非空参数或绑定属性视为具有[Required]
属性。
通常,如果int
类型属性的输入(例如Stock
)大于0而不是null
,则我们有效。