开发者

Elegant Method to Validate Submitted Foreign Keys

开发者 https://www.devze.com 2023-02-08 11:18 出处:网络
I have lately read several articles and q开发者_运维技巧uestions 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 li

I have lately read several articles and q开发者_运维技巧uestions 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号