MVC3 对来自引用程序集的实体进行远程验证?
如果我在单独的程序集中定义了一个实体类(例如来自实体框架),是否可以向属性添加远程验证?
如果该类是在我的 MVC 项目中定义的,我只需向该属性添加一个 Remote 属性,但当该类在单独的程序集中定义时,我无法执行此操作。我确实在 EF 项目中定义了其他数据注释,但在那里添加远程注释没有任何意义。
我可能可以手动将 jquery 验证规则添加到我的视图中,但我想知道是否有办法让 MVC 框架为我做这件事。
我在网上找到的每个示例都假设您正在使用 MVC 项目中定义的实体,可以轻松添加远程验证。在我的视图中使用我的 EF 实体作为我的模型类是错误的吗?创建与我的 EF 类具有所有相同属性和所有相同数据注释的新实体,并且必须在两者之间来回转换,这似乎是浪费精力。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 MVC 项目中使用 ViewModel 并将远程属性添加到模型中,或者如果您在另一个程序集中使用实体,为什么不能将远程属性添加到这些实体的属性中?我不确定我是否明白为什么这样做没有意义。
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.
这绝对是单独视图模型类的情况。如果您希望 ASP.NET MVC 特定功能和实体同时存在于单独的程序集中您必须使用视图模型。要简化实体和视图模型之间的映射,您可以使用 AutoMapper。
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.