This project is read-only.

How to access ModelMetadata in Web API 2 Controller?

Topics: ASP.NET Web API
Dec 13, 2013 at 4:50 AM
I'm trying to get the model metadata for a property on my model in my web api 2 controller. I'm trying to do something similar to what NameFor(m => m.Property) in MVC because I need to add a custom validation error to the model state with ModelState.AddModelError. But I don't want to use a hard coded string that can easily get messed up with refactorings. Thanks.
Dec 13, 2013 at 7:45 PM
Instead of doing the validation and manipulating ModelState in your action have you considered instead implementing the validation logic on your model type and letting the validation system handle running the validation logic and collecting the errors for you? You can add custom validation attributes to your model type or implement IValidatableObject.
Dec 14, 2013 at 9:15 AM
I am implementing 99%+ of the validation logic on the model. This is one very special case that has to do a lot of Db logic to validate so I was hoping to just put this in the controller. Any way to get the name in there? Thanks.