Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

ModelState always keep Unvalidated state when Model implements IEnumerable<>

Open marcusien opened this issue 10 years ago • 1 comments

The ModelState is always non valid if my ViewModel implements the IEnumerable<> interface. Not any error is raised, just all the ModelStateNode keep the Unvalidated state.

I put a little sample here

A same ViewModel with or without IEnumerable implementation does not behave the same way.

marcusien avatar Jul 07 '16 09:07 marcusien

I doubt this model would bind as you prefer, let alone validate both properties and members of the collection.

A model type which implements IEnumerable<T> or ICollection<T> is handled by a CollectionModelBinder or one of its subclasses while POCO types are handled by a ComplexTypeModelBinder. If you want to bind properties of collection types, create a custom IModelBinder and associated IModelBinderProvider.

To validate properties of collection types, create a custom IValidationStrategy and Add() a ValidationStateEntry referencing an instance of that type in your custom IModelBinder. The DefaultCollectionValidationStrategy or DefaultComplexObjectValidationStrategy won't do both.

dougbu avatar Jul 10 '16 02:07 dougbu