I have a contact viewmodel which I use to strongly type my partial view “Contact”. In one of my form I got to use the same Contact partial view for two areas ex. Customer as well as Vendor.
Is there a way to give two different error message ex: Please enter “Customer” Name and Please enter “Vendor” Name using the same view model?
Now it shows two general error messages “Please enter name” (Which is what is specified for the required attribute error message).
Any help or suggestions are greatly appreciated.
TIA.
Regards,
Raja
The best pattern here is to create a second view model, don’t try to simply save on code if they really serve two different purposes.
A second option is your model implements IValidateableObject interface and adds this custom message as required
A third option is in your controller if you detect the field isn’t there simply use ModelState.AddModelErrors. You could make it not a required field and check in your controller if its present or query modelstate’s errors to see if its there, clear it out and re-add it with the updated text.