DataAnnotations不适用于伙伴类.以下代码始终验证为true.为什么?
var isValid = Validator.TryValidateObject(new Customer(),Context,results,true);
这是好友班.
public partial class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
public class CustomerMetaData
{
[Required(ErrorMessage = "You must supply a name for a customer.")]
public string Name { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个有相同问题的主题,但没有答案. 链接文字
silverlight asp.net-mvc .net-4.0 buddy-class data-annotations
我正在使用VS2008 SP1,WCF Ria Service 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不行
public partial class Person
{
private string _Name;
public string Name
{
set{_Name = value;} …Run Code Online (Sandbox Code Playgroud)