验证提交的外键的优雅方法
我最近阅读了几篇有关 ASP.NET MVC 中的模型绑定和验证的文章和问题。到目前为止,我错过的是给定外键 ID 的验证(例如相关项目的下拉列表)。
如果我们考虑以下情况:
存在一个具有一组文件夹的实体(即网站区域),并且允许用户将条目放入该文件夹中,但只能放入属于用户有权访问的网站区域的文件夹中到。
那么,什么是一种优雅的方法来验证给定的外键值是否属于属于相关网站区域的文件夹集呢?
我认为使用自定义模型绑定器和/或可以访问数据存储库的自定义验证器来执行此检查是明智的?
I have lately read several articles and questions about Model Binding and Validation in asp.net MVC. What i have missed so far was the validation of a given foreign key id (drop-down list of related items for example).
If we consider the following situation:
There is an Entity (i.e. a Website Area) that features a set of folders, and the user is allowed to put entries into that folder, but only into the folders belonging to the Website Area the user has access to.
So what could be an elegant method to validate if the given foreign key value belongs to the set of folders that belong to the Website Area in question?
I assume it would be smart to perform this check by using a custom Model Binder and or a custom Validator which has access to the data repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不让数据库完成它的工作并让它验证 FK。在您的控制器中,您可以处理错误并按您认为合适的方式显示回视图。
Why not just let the database do its work and let it validate FK's. In your controller you can handle the error and display back to view as you see fit.