0

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 two.

gideon
  • 19,329
  • 11
  • 72
  • 113
Glazed
  • 2,048
  • 18
  • 20

2 Answers2

0

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.

Joe Cartano
  • 2,997
  • 3
  • 22
  • 40
  • I don't want to add the Remote attribute there because the entity project is not supposed to reference the MVC assembly or have web-specific things in it. Adding it there would mean that my EF project knows what URL I want to use when Remote validation fires. That sort of decision shouldn't be made in that project. – Glazed Apr 13 '11 at 16:53
  • You should probably use ViewModels then. This will give you more control over what data is being passed to the view and let you avoid referencing the MVC project where your entities are defined. Otherwise I don't think there is anything in the framework that will allow you to do this. – Joe Cartano Apr 13 '11 at 17:07
  • I tried this, and it seems acceptable. It's working out a little better than I expected because I can reuse the MetadataType from my EF project on my ViewModel. Then I can also add the Remote attributes. This way I don't have to repeat all the metadata that I've already defined in my EF project, and if it ever changes, I don't have to worry about keeping it in sync with my ViewModel's metadata. **I take that back.** It's not firing the Remote validation. :( I guess you can't add mroe validations that aren't in the MetadataType. – Glazed Apr 13 '11 at 17:23
  • I'm mistaken. Other additional annotations are working, just not the Remote one. I'll poke at it. – Glazed Apr 13 '11 at 17:29
  • It's firing. I was just being foolish. I had the parameter names in my action method set wrong. I probably shouldn't have said it's not firing unless I've actually set a breakpoint and checked. With this particular validation (duplicate checking), it was happily checking a null value and returning true. – Glazed Apr 13 '11 at 17:40
0

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This is the direction I am taking, as others have suggested. Thanks for the link to AutoMapper! It looks very nice. However, in this case I have a whole two properties to map on one entity. I don't think I will use AutoMapper. However, I will bookmark it. Thanks! – Glazed Apr 13 '11 at 17:43
  • Sure, for a single entity with two properties it would be overkill. – Ladislav Mrnka Apr 13 '11 at 17:51