0

I am getting an error using the [Remote] attribute on a [MetadataType] class. I get the following error: Error 15 Attribute 'Remote' is not valid on this declaration type. It is only valid on 'property, indexer' declarations.

I understand what the error is saying, I just don't understand why [Remote] won't work but other attributes work fine.

[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;
} 
4

1 回答 1

1

在 Remote 属性的定义处:

  [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)
    {
    }
}

它对我有用。

于 2013-06-09T15:15:25.943 回答