元数据类型问题
我正在使用 VS2008 SP1、WCF Ria 服务 2009 年 7 月 CTP。我发现 MetadataType 在部分类模式下不起作用,真的不知道我错过了什么:
工作:-
public partial class Person
{
private string _Name;
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}
不工作
public partial class Person
{
private string _Name;
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}
public partial class PersonMetadata
{
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name;
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}
I'm using VS2008 SP1, WCF Ria Service July 2009 CTP. I found out that MetadataType does not work in partial class mode, really don't know what I have missed out:
Work:-
public partial class Person
{
private string _Name;
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}
Not Work
public partial class Person
{
private string _Name;
public string Name
{
set{_Name = value;}
get{return _Name;}
}
}
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}
public partial class PersonMetadata
{
[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name;
}
class Program
{
static void Main(string[] args)
{
Person p = new Person { Name="123432" };
List res = new List();
Validator.TryValidateObject(p,new ValidationContext(p,null,null),
res,true);
if (res.Count > 0)
{
Console.WriteLine(res[0].ErrorMessage);
Console.ReadLine();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:我在这里找到了答案:http:// /forums.silverlight.net/forums/p/149264/377212.aspx
在验证之前,您需要手动注册元数据类:
(原始答案如下)
问题并不具体对于您的部分类, Validator.TryValidateObject 似乎无法识别 MetaDataType 属性。我有同样的问题 - MVC 2 中的内置验证可以识别元数据类,但 TryValidateObject 不能。
看看这些:
使用 Validator 类验证 DataAnnotations
当我使用 Validator.TryValidateObject 时验证不起作用
作为旁注,我不知道是否有必要,但我见过的元数据类的所有示例都在每个属性上使用默认的获取/设置:
EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx
Before validating, you need to manually register the metadata class:
(Original answer follows)
The problem isn't specifically with your partial class, it's that Validator.TryValidateObject doesn't seem to recognize the MetaDataType attribute. I have the same problem - the built-in validation in MVC 2 recognizes the metadata class, but TryValidateObject doesn't.
See these:
Validating DataAnnotations with Validator class
Validation does not work when I use Validator.TryValidateObject
As a side note, I don't know if it's necessary, but all examples I've seen for metadata classes employ the default get/set on each property:
非常感谢杰里米·格伦瓦尔德(Jeremy Gruenwald)的上述回答……我完全被困在这个问题上了。
我想基于此解决方案创建一个标准验证类,但我不想必须传递元数据类类型,因为它感觉很丑。
为了实现这一目标,我创建了一个静态类,它对自定义属性进行查找以获取元数据类类型,然后在返回验证结果之前注册该类。
用法很简单:
Many thanks to Jeremy Gruenwald for the answer above... I was completely stuck on this one.
I wanted to create a standard validation class based on this solution, but I didn't want to have to pass in the metadata class type because it just felt ugly.
To achieve this I created a static class which does a lookup on the custom attributes to get the metadata class type and then registers that class before returning the validation results.
usage is a simple as:
如果您正在使用 WPF 和 EF,这对我来说一直有效...
以及实现它的基类...
If you are working with WPF and EF, this has always worked for me...
And the base class that makes it happen...