2

I have a view model class that derives from IValidateableObject, and I am using the Validate method with ValidationContext to evaluate it from my controller.

I would like to be able to test against more than the properties of the view model class itself in ObjectInstance. I could probably add these things to the model and then everything would be easy, but I don't feel like these things really belong in the view model class, as they are more about the current state of that object.

I was wondering if the Items collection in ValidationContext is an appropriate place to store such things... The only problem is that I can't seem to find a resource that explains how I would go about adding things to that dictionary.

Does that make sense? Can anyone provide an explanation for how to populate ValidationContext.Items prior to checking the ModelState of an object?

UPDATE #1 - I found the following on an old blog entry about EF CTP5 (), but I'm not sure how to use this information:

Therefore it is possible to use custom validation logic that can completely replace built-in validation or filter out some validation errors returned by built-in validation. One more little detail about DbContext.ValidateEntity(http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx) method is the second parameter of this method which looks like this:

IDictionary<object, object> items

By default its value is always null. However overriding DbContext.ValidateEntity() allows to pass a non-null value to this method by calling:

return base.ValidateEntity(entityEntry, myItems);

UPDATE #2 - I tried to go down the path of using a custom model binder, but I'm hitting a wall with that too - there does not seem to be a way to simply add items to the ValidationContext and then call the base.OnModelUpdated(controllerContext, bindingContext) method.

tereško
  • 58,060
  • 25
  • 98
  • 150
DMC
  • 361
  • 4
  • 15
  • This seems like it is also closely related to what I want to do, but I'm having trouble putting the pieces together: http://stackoverflow.com/questions/12918194/custom-validationcontext-for-ef – DMC Jun 28 '13 at 14:46

1 Answers1

0

If you are after building your own model validator, take a look at this question. If that sounds like too much work, maybe try the FluentValidation project, which will let you add custom validation rules to your view model and store them in their own class.

Community
  • 1
  • 1
meataxe
  • 969
  • 13
  • 33
  • Really I just want to understand how the Items collection in ValidationContext is intended to be used. Would it make sense to have objects in that dictionary that I can use to help me validate my view model? – DMC Jun 28 '13 at 13:04
  • 1
    Looking at the FluentValidation source, it subclasses MVC's `ModelValidatorProvider` which get added to `ModelValidatorProviders.Providers` in global.asax. It doesn't use any DbContext/EF stuff, and there's a few articles I was able to google about how the `ModelMetadataProvider` and the `ModelValidatorProvider` work. – meataxe Jun 30 '13 at 20:47
  • I appreciate the comment. I looked at the FluentValidation source on github. I still do not see where I can influence the ValidationContext object that is passed to my IValidatableObject, although I do see the sections of this code that make use of ModelValidatorProvider. – DMC Jul 02 '13 at 19:50
  • I'm not sure I can add any more - you've reached the limit of my understanding. If you do make any progress, please update this question with your findings. – meataxe Jul 03 '13 at 01:30