ASP.NET MVC 2 模型验证可以处理属性包吗?

发布于 2024-10-06 16:51:45 字数 282 浏览 2 评论 0原文

我一直在阅读有关 ASP.NET MVC 2 模型验证的内容,并且我很喜欢它会自动将相同的规则应用于客户端和服务器端对象。但是,我见过的所有示例都使用了自定义模型对象。我正在研究的设计使用属性包(键->值对的列表,如字典),每个属性包可以有不同的验证。

例如,

  • 发票编号:字母数字,正好 10 个字符
  • 用户名:少于 50 个字符。
  • 发票金额:钱。

无论是否使用自定义验证引擎,ASP.NET MVC 2 模型验证都可以验证这些规则吗?

I've been reading about ASP.NET MVC 2 Model Validation and I'm in love with the fact that it will automatically apply the same rules to your client side and server side objects. However, all the examples I've seen have used custom model objects. A design I'm working on uses property bags (a list of key->value pairs, like a dictionary) that can each have different validations.

For example

  • Invoice Number: AlphaNumeric, Exactly 10 characters
  • User Name: Less than 50 characters.
  • Invoice Amount: Money.

Can ASP.NET MVC 2 Model Validation validate those rules, with or without a custom validation engine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绳情 2024-10-13 16:51:45

可以进行 ASP.NET MVC 2 模型验证吗
验证这些规则,有或没有
自定义验证引擎?

抱歉我上次的回答有点含糊。对于任何长度或字符数验证正则表达式是关键。然而,你的情况有点不同。

这里使用 mvc 2 模型验证可以做什么:

   1. using System;  
   2. using System.ComponentModel.DataAnnotations;  
   3.   
   4. namespace FunWithMvc2RC2  
   5. {  
   6.     public class Test  
   7.     {  
   8.         // StringLenght  
   9.         [StringLength(5, ErrorMessage = "Maximum 25 Characters")]  
  10.         public string StringLength { get; set; }  
  11.   
  12.         // Required  
  13.         [Required(ErrorMessage = "Required Field")]  
  14.         public string Required { get; set; }  
  15.   
  16.         // Required and StringLenght  
  17.         [Required(ErrorMessage = "Required Field")]  
  18.         [StringLength(5, ErrorMessage = "Maximum 25 Characters")]  
  19.         public string Combos { get; set; }  
  20.   
  21.         // Range Attribute  
  22.         [Range(1, 31, ErrorMessage = "Minimum 1; Maximum 31")]  
  23.         public int Range { get; set; }  
  24.   
  25.         // RegularExpression Attribute  
  26.         [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "Invalid Email Address")]  
  27.         public string Regex { get; set; }  
  28.   
  29.         // Custom Regular Expression EmailAttribute  
  30.         [Email(ErrorMessage = "Email Validation")]  
  31.         public string Email { get; set; }  
  32.     }  
  33. }  

所以我想说,对于使用密钥列表和所有内置验证的自定义内容,使用 MVC 2 中包含的默认验证规则是不可能的。您需要制定自己的验证规则。请参阅自定义模型验证 http://haacked.com/archive/ 2009/11/19/aspnetmvc2-custom-validation.aspx

希望有帮助。

Can ASP.NET MVC 2 Model Validation
validate those rules, with or without
a custom validation engine?

Sorry about my last answer, it was a bit vague. For any lenght or character count validation regulars expressions are the key. However your situation is a bit different.

Here what can be done using mvc 2 model validation :

   1. using System;  
   2. using System.ComponentModel.DataAnnotations;  
   3.   
   4. namespace FunWithMvc2RC2  
   5. {  
   6.     public class Test  
   7.     {  
   8.         // StringLenght  
   9.         [StringLength(5, ErrorMessage = "Maximum 25 Characters")]  
  10.         public string StringLength { get; set; }  
  11.   
  12.         // Required  
  13.         [Required(ErrorMessage = "Required Field")]  
  14.         public string Required { get; set; }  
  15.   
  16.         // Required and StringLenght  
  17.         [Required(ErrorMessage = "Required Field")]  
  18.         [StringLength(5, ErrorMessage = "Maximum 25 Characters")]  
  19.         public string Combos { get; set; }  
  20.   
  21.         // Range Attribute  
  22.         [Range(1, 31, ErrorMessage = "Minimum 1; Maximum 31")]  
  23.         public int Range { get; set; }  
  24.   
  25.         // RegularExpression Attribute  
  26.         [RegularExpression(@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$", ErrorMessage = "Invalid Email Address")]  
  27.         public string Regex { get; set; }  
  28.   
  29.         // Custom Regular Expression EmailAttribute  
  30.         [Email(ErrorMessage = "Email Validation")]  
  31.         public string Email { get; set; }  
  32.     }  
  33. }  

So i would say that for custom stuff such using a list of key and all with those build-in validatioin, this isnt possible using default validation rules included in MVC 2. You will need to make your own validation rules. See custom model validation at http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx.

Hope that help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文