简体   繁体   English

用户定义的验证

[英]User defined validation

I would like to kwnow if it possible for users to add validation rules at runtime. 我想知道用户是否可以在运行时添加验证规则。 I have a system that will be sold to clients that could be anywhere in the world. 我有一个可以出售给世界各地客户的系统。 So in some countries certain fields is required and in other those fields are not required. 因此,在某些国家/地区中,某些字段是必需的,而在其他国家/地区中,则不需要。 So i thought I would add functionality so that when i implement the system, the fields that needs to be validated could be set by me, without the need to add the data-annotations to the properties, then recompile the entire system. 因此,我想我应该添加一些功能,以便在实施系统时可以由我设置需要验证的字段,而无需在属性中添加数据注释,然后重新编译整个系统。

It will be an ASP.NET MVC application using Entity Framework 这将是使用Entity Framework的ASP.NET MVC应用程序

regards 问候

You can do this in the following way 您可以通过以下方式进行操作

  1. Use data-annotations with the properties 在属性中使用数据注释
  2. And while using the annotation check on your view there you can check which country it is by using a simple if else condition, and show the checks only at the required country views 在视图上使用注释检查时,您可以使用简单的if else条件检查所在的国家/地区,并仅在所需的国家/地区视图上显示检查

     if(someCondition) { @Html.ValidationMessageFor(model => @Model.User.Username) } 

尝试fluentvalidation http://fluentvalidation.codeplex.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM