2013-01-20 38 views
0

我在[MetadataType]類中使用[Remote]屬性出現錯誤。我收到以下錯誤: 錯誤15屬性'Remote'在此聲明類型中無效。它只對'property,indexer'聲明有效。在[MetadataType]中使用'Remote'屬性

我明白錯誤在說什麼,我只是不明白爲什麼[遠程]不能工作,但其他屬性工作正常。

[MetadataType(typeof(StudentRowMeta))] 
public class StudentRow 
{ 
    public string Login { get; set; } 
} 

public class StudentRowMeta 
{ 
    [Required(ErrorMessage = "Please Enter Login")] 
    [StringLength(50, ErrorMessage = "Login can not be more than 50 characters")] 
    [Remote("IsLoginAvailable", "Validation")] 
    public object Login; 
} 

回答

1

遠程屬性的定義:

[AttributeUsage(AttributeTargets.Property)] 
    public class RemoteAttribute : ValidationAttribute, IClientValidatable { ... 

您只能使用原來的RemoteAttribute財產。但是,沒有什麼能阻止使用後代的新屬性定義:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] 
public class MyRemoteAttribute : RemoteAttribute 
{ 
    public MyRemoteAttribute(string action, string controller) 
     : base(action, controller) 
    { 
    } 
    public MyRemoteAttribute(string action, string controller, string area) 
     : base(action, controller, area) 
    { 
    } 
} 

它已經爲我工作的領域。

相關問題