MVC3 remote validation on an entity from a refrenced assembly?
If I have an entity class defined in a separate assembly (like from Entity Framework), is it possible to add remote validation to a property?
If the class were defined in my MVC project I would simply add a Remote attribute to the property, but I can't do that when the class is defined in a separate assembly. I do have other data annotations defined in my EF project, but it doesn't make any sense to add a Remote annotation there.
I can probably just add the jquery validation rule to my view by hand, but I'd like to know if there's a way to let the MVC framework do it for me.
Every example I've found on the web assumes you're using an entity defined in your MVC project where it's easy to add remote validation. Is it a mistake to use my EF entities as my model classes in my views? It seems like a waste of effort to make new entities with all the same properties, and all the same data annotations as my EF classes, and have to translate back and forth between the 开发者_JS百科two.
You can use ViewModels in your MVC project and add the remote attribute to your models there or if you are using entities in another assembly why can't you add the remote attribute to properties of those entities? I am not sure I see why it doesn't make sense to do that.
This is definitely a case for a separate view model class. If you want ASP.NET MVC specific features and entities in a separate assembly in the same time you must use a view model. To simplify mapping between the entity and the view model you can use AutoMapper.
精彩评论