Asp.net mvc 4 使用保存在数据库中的元数据进行用户自定义验证
我正在开发一个应用程序,它应该根据数据库中保存的一些元数据来验证模型。这样做的目的是允许管理员根据客户的偏好更改某些模型的验证方式,而无需更改代码。更改应用于整个应用程序,而不是访问它的特定用户。现在,它是如何改变的并不重要。它们可以直接在数据库上修改,也可以使用应用程序修改。想法是它们应该是可定制的 假设我有一个属性为“Name”类型为“string”的模型“Person” 我的应用程序使用这个模型,它分布并安装在多个服务器上。他们每个人都是独立的。一些用户可能希望名称最多包含30个字母,并且在创建新“人”时需要该名称,其他用户可能希望名称包含25个字母,而不是必需的。通常,这可以通过使用数据注释来解决,但这些注释是在编译时进行计算的,并且以某种方式是“硬编码”的 简而言之,我想找到一种方法来定制模型如何验证,并将其存储在数据库中,而无需修改应用程序代码Asp.net mvc 4 使用保存在数据库中的元数据进行用户自定义验证,asp.net-mvc-4,model-validation,Asp.net Mvc 4,Model Validation,我正在开发一个应用程序,它应该根据数据库中保存的一些元数据来验证模型。这样做的目的是允许管理员根据客户的偏好更改某些模型的验证方式,而无需更改代码。更改应用于整个应用程序,而不是访问它的特定用户。现在,它是如何改变的并不重要。它们可以直接在数据库上修改,也可以使用应用程序修改。想法是它们应该是可定制的 假设我有一个属性为“Name”类型为“string”的模型“Person” 我的应用程序使用这个模型,它分布并安装在多个服务器上。他们每个人都是独立的。一些用户可能希望名称最多包含30个字母,并且
另外,最好使用jquery验证,并尽可能少地请求数据库(/service)。除此之外,我不能使用任何已知的ORM,如EF。您可以创建一个自定义验证属性,通过检查存储在数据库中的元数据进行验证。自定义验证属性易于创建,只需扩展
System.ComponentModel.DataAnnotations.ValidationAttribute
并重写IsValid()
方法即可
要获取使用jQuery验证的客户端规则,您需要为扩展System.Web.Mvc.DataAnnotationsModelValidator的自定义验证属性类型创建自定义适配器。然后需要在Global.asax
的onaapplicationstart()
方法中注册此类
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(YourCustomValidationAttribute), typeof(YourCustomAdapter));
下面是一个适配器示例:
public class FooAdapter : DataAnnotationsModelValidator<FooAttribute>
{
/// <summary>
/// This constructor is used by the MVC framework to retrieve the client validation rules for the attribute
/// type associated with this adapter.
/// </summary>
/// <param name="metadata">Information about the type being validated.</param>
/// <param name="context">The ControllerContext for the controller handling the request.</param>
/// <param name="attribute">The attribute associated with this adapter.</param>
public FooAdapter(ModelMetadata metadata, ControllerContext context, FooAttribute attribute)
: base(metadata, context, attribute)
{
_metadata = metadata;
}
/// <summary>
/// Overrides the definition in System.Web.Mvc.ModelValidator to provide the client validation rules specific
/// to this type.
/// </summary>
/// <returns>The set of rules that will be used for client side validation.</returns>
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return new[] { new ModelClientValidationRequiredRule(
String.Format("The {0} field is invalid.", _metadata.DisplayName ?? _metadata.PropertyName)) };
}
/// <summary>
/// The metadata associated with the property tagged by the validation attribute.
/// </summary>
private ModelMetadata _metadata;
}
公共类FooAdapter:DataAnnotationsModelValidator
{
///
///MVC框架使用此构造函数检索属性的客户端验证规则
///与此适配器关联的类型。
///
///有关正在验证的类型的信息。
///处理请求的控制器的ControllerContext。
///与此适配器关联的属性。
公共FooAdapter(ModelMetadata元数据、ControllerContext上下文、FooAttribute属性)
:base(元数据、上下文、属性)
{
_元数据=元数据;
}
///
///重写System.Web.Mvc.ModelValidator中的定义,以提供特定于客户端的验证规则
///这种类型的。
///
///将用于客户端验证的规则集。
公共重写IEnumerable GetClientValidationRules()
{
返回新[]{new ModelClientValidationRequiredRule(
Format(“该{0}字段无效。”,_metadata.DisplayName???_metadata.PropertyName))};
}
///
///与验证属性标记的属性关联的元数据。
///
私有模型元数据;
}
如果要异步调用服务器端验证,这也可能很有用
public class FooAdapter : DataAnnotationsModelValidator<FooAttribute>
{
/// <summary>
/// This constructor is used by the MVC framework to retrieve the client validation rules for the attribute
/// type associated with this adapter.
/// </summary>
/// <param name="metadata">Information about the type being validated.</param>
/// <param name="context">The ControllerContext for the controller handling the request.</param>
/// <param name="attribute">The attribute associated with this adapter.</param>
public FooAdapter(ModelMetadata metadata, ControllerContext context, FooAttribute attribute)
: base(metadata, context, attribute)
{
_metadata = metadata;
}
/// <summary>
/// Overrides the definition in System.Web.Mvc.ModelValidator to provide the client validation rules specific
/// to this type.
/// </summary>
/// <returns>The set of rules that will be used for client side validation.</returns>
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return new[] { new ModelClientValidationRequiredRule(
String.Format("The {0} field is invalid.", _metadata.DisplayName ?? _metadata.PropertyName)) };
}
/// <summary>
/// The metadata associated with the property tagged by the validation attribute.
/// </summary>
private ModelMetadata _metadata;
}