Mike
Mike

Reputation: 114

Using 'Remote' attribute with [MetadataType]

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

Upvotes: 0

Views: 798

Answers (1)

Kornel Regius
Kornel Regius

Reputation: 81

At the definition of Remote attribute:

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

You can only use the original RemoteAttribute with property. But, nothing prevents that a new attribute definition of descendant of use:

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

It has worked for me with field.

Upvotes: 1

Related Questions