I wanted to do this validation in add and edit product
I set the [Required] attribute for both item and product but the validation doesn't work and it gives me this error when all fields are empty and when I hit register.
ProductViewModel=>
[Required(ErrorMessage = "لطفا {0} را وارد کنید.")]
[StringLength(27, ErrorMessage = "کد پستی باید 10 رقمی باشد.", MinimumLength = 5)]
[DisplayName("نام و نام خانوادگی")]
public string Name { get; set; }
[Required(ErrorMessage = "لطفا {0} را وارد کنید.")]
[StringLength(97, ErrorMessage = "کد پستی باید 10 رقمی باشد.", MinimumLength = 10)]
[DisplayName("توضیحات")]
public string Description { get; set; }
itemViewModel=>
[Required(ErrorMessage = "لطفا {0} را وارد کنید.")]
[DisplayName("قیمت")]
public int Price { get; set; }
[Required(ErrorMessage = "لطفا {0} را وارد کنید.")]
[DisplayName("تعداد")]
public int QuantityInStock { get; set; }
CodePudding user response:
This is an exception caused by the value you store to the database being null.
Add the following code to the Page to make the Required
attribute take effect:
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}