asp.net mvc 3 - IValidatableObject and Nullable value types -
i have struct implements ivalidatableobject, use properties on view model. validation works fine non-nullable properties, if add property of type nullable<mystruct> ivalidatableobject.validate method never called property, if has value.
is there way trigger validation on nullable properties too, or ivalidatableobject not intended used value types?
thanks
additional information:
i wanted able specify units when entering data in form, e.g. 10m, 100km, 1cm etc. wanted validate entered data within range , of correct format. struct converts string potentially different units (100cm/100m/1km) decimal property has same unit (1/100/1000) , vice-versa.
i used ivalidatableobject validation because validation same instance of mystruct - didn't want add attribute each time used.
my model this:
public class mymodel { public mystruct ab {get; set;} public mystruct? cd {get; set;} } my view typed view using model, renders struct text box using display template. when form posted, have custom model binder convert entered string struct.
my controller action looks like:
public actionresult myaction(mymodel model) { //do stuff } the model bound ok properties of type mystruct , nullable<mystruct>.
it seems not possible.
i dug mvc source. in system.web.mvc.dataannotationsmodelvalidatorprovider there following code:
// produce validator if type supports ivalidatableobject if (typeof(ivalidatableobject).isassignablefrom(metadata.modeltype)) { //stuff } according this rejected bug report, nullable<t> cannot assigned interface t implements:
"in short, "datetime?" value can unboxed datetime or iformattable value. cannot assigned datetime or iformattable location."
thus no validators returned.
looks have way.
Comments
Post a Comment