【发布时间】:2011-12-18 22:06:15
【问题描述】:
我有以下 JSON:
validate = {
'(\\.org|\\.info|\\.biz|\\.name)$': [
{ 'type': 'size', 'pattern': /^.{3,64}$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
],
'.*': [
{ 'type': 'general', 'pattern': /^[^\.-].*[^\.-]$/, 'message': 'Your domain name shouldn\'t contain . or - at the beginning or the end.' },
{ 'type': 'characters', 'pattern': /^[abcdefghijklmnopqrstwuvxyz0123456789]+$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
]
};
并尝试像这样使用:
var validate = new Dictionary<string, dynamic> {
{
@"(\.org|\.info|\.biz|\.name)$",
new {
Type = "size",
Pattern = @"^.{3,64}$",
Message = "Your domain can have at max 26 characters and at least 3."
}
}
};
动态对象的键是域扩展的正则表达式模式,Pattern 键中的正则表达式应该与域名匹配。
但我不知道如何将 2 个验证类型放入 Dictionary 的 dynamic 部分。
以前有没有人做过类似的事情,或者这很愚蠢,我应该以另一种方式做?
这样做的目的是我可以将字典序列化为 Json。
【问题讨论】:
-
你想要完成什么?
-
我正在尝试用它创建一个自定义数据注释,然后我想使用 asp-net-mvc3 将字典中的内容作为 Json 发送到视图,这样我就可以使用验证规则C# 代码和每次我们更改 C# 验证时,客户端都会跟随。
标签: c# javascript json asp.net-mvc-3 dictionary