ASP.NET MVC 2 模型验证可以处理属性包吗?
我一直在阅读有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉我上次的回答有点含糊。对于任何长度或字符数验证正则表达式是关键。然而,你的情况有点不同。
这里使用 mvc 2 模型验证可以做什么:
所以我想说,对于使用密钥列表和所有内置验证的自定义内容,使用 MVC 2 中包含的默认验证规则是不可能的。您需要制定自己的验证规则。请参阅自定义模型验证 http://haacked.com/archive/ 2009/11/19/aspnetmvc2-custom-validation.aspx。
希望有帮助。
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 :
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.