如何自动生成 DataValidation“Buddy” ASP.NET MVC3 中数据库中的类?
我有一个网站,其中的视图直接从实体框架创建的对象生成。阅读 此博客后Scott GU 看来让 DataValidation 工作的最好方法是从 POCO 对象开始。 (在链接上搜索“好友类”一词)
我可以以某种方式自动生成我的好友类吗?还是我一开始涉足 MVC 就走错了路?
I have a website that has the views generated directly from objects created by Entity Framework. After reading this blog by Scott GU it seems the best way to get DataValidation to work is to have started with POCO objects instead. (search for the word Buddy Class on the link)
Can I auto-generate my buddy classes somehow? Or did I start my foray into MVC on the wrong foot?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕情况确实如此,下面这句话证实了这一点:
。您不应直接在视图中使用 EF 模型。拥有视图模型被认为是很好的做法。这些类是专门为满足给定视图的需要而设计的,并且可以包含格式化和验证。典型的场景可能如下:
当控制器操作从视图接收视图模型、检查模型状态是否有效、将其映射回模型并将模型传递到存储库时,此场景也适用。
I am afraid that this might be the case and the following sentence confirms it:
You shouldn't use your EF models in the views directly. It is considered good practice to have view models. Those are classes that are specifically designed to the needs of a given view and could contain formatting and validation. A typical scenario might be the following:
This scenario works also when a controller action receives a view model from the view, checks whether the modelstate is valid, maps it back to a model and passes the model to a repository.