Hello
I'm having problems with getting my custom dataannotations to work, I'm trying to add a validation-attribute that validates that the UsergroupName for a Customer (CustomerID) is unique.
[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }
public class UsergroupMetaData
{
[Required()]
public object CustomerID { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
public object UsergroupName { get; set; }
[UniqueUsergroupName(????)]
// what to put here?
}
public class UniqueUsergroupName : ValidationAttribute
{
UsergroupRepository _rep = new UsergroupRepository();
public override bool IsValid(object value, int customerID)
{
var x = _rep.GetUsergroups().ByUsergroupName(value).ByCustomerID(customerID);
// what to put here?
return false;
}
}
the IsValid should return false if the "count >0".
How do I fix this one so it works. GetUsergroups() returns IQueryable.
EDIT:
[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }
public class UsergroupMetaData
{
public object CustomerID { get; set; }
[Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
[UniqueUsergroupName(ErrorMessageResourceType= typeof(Resources), ErrorMessageResourceName = "UsergroupNameExists")]
public object UsergroupName { get; set; }
}
public class UniqueUsergroupName : ValidationAttribute
{
UsergroupRepository _rep = new UsergroupRepository();
public override bool IsValid(object value, int customerID)
{
int usergroups = _rep.GetUsergroups().ByCustomerID(customerID).ByUsergroupName(value.ToString()).Count();
return usergroups >0;
}
}
How can I pass the current CustomerID as a parameter?
/M