There are some properties in my view model that are optional when saving, but required when submitting. In a word, we allow partial saving, but the whole form is submitted, we do want to make sure all required fields have values.
The only approaches I can think of at this moment are:
Manipulate the ModelState errors collection.
The view model has all [Required]
attributes in place. If the request is partial save, the ModelState.IsValid
becomes false
when entering the controller action. Then I run through all ModelState
(which is an ICollection<KeyValuePair<string, ModelState>>
) errors and remove all errors raised by [Required]
properties.
But if the request is to submit the whole form, I will not interfere with the ModelState
and the [Required]
attributes take effect.
Use different view models for partial save and submit
This one is even more ugly. One view model will contain all the [Required]
attributes, used by an action method for submitting. But for partial save, I post the form data to a different action which use a same view model without all the [Required]
attributes.
Obviously, I would end up with a lot of duplicate code / view models.
The ideal solution
I have been thinking if I can create a custom data annotation attribute [SubmitRequired]
for those required properties. And somehow make the validation ignores it when partial saving but not when submitting.
Still couldn't have a clear clue. Anyone can help? Thanks.