I am working on .net core 3.1. My question is say for egg, I have one model with name Product with data annotation validation for images. I am using this model in both insert and update pages. I was suppose to validate the images only in insert page not in update page. I thought of writing another model for update without data annotations for image field. But I need to reuse the same model as expected. Is there any possible way?
Thanks in advance.
Asked
Active
Viewed 61 times
0
barath thangam
- 77
- 3
-
I have not tested it, but I wonder if you could inherit from the non-annotated model and add the annotation to the child class. You would not be duplicating code – Cleptus May 20 '22 at 10:01
-
If you look at this question [Conditionally required property using data annotations](https://stackoverflow.com/questions/26354853/conditionally-required-property-using-data-annotations) and your model has an autonumeric/identity column, that could serve you to do the insert/update condition check. – Cleptus May 20 '22 at 10:21
1 Answers
0
I think you can use the model with data annotation validation in both insert and update pages,and only use <partial name="_ValidationScriptsPartial" /> in insert page,so that the images will not be validated in the update page.Insert Page:
@section Scripts{
<partial name="_ValidationScriptsPartial" />
}
Yiyi You
- 12,796
- 1
- 4
- 13