I’ve been programming asp.net for, oh, a couple of days now. Here’s a question I can’t even begin to figure out for myself.
I hope it’s obvious from the code what I want to accomplish, and I have, but it’s not pretty. Furthermore I’d like to use it on whatever table, whatever field, i.e. check the uniqueness of a value against a table and field I specify, passing it all into the attribute constructor.
public class UniqueEmailAttribute : ValidationAttribute
{
public UniqueEmailAttribute()
{
}
public override Boolean IsValid(Object value)
{
//not pretty. todo: do away with this.
var db = new CoinDataContext();
int c = db.Emails.Count(e => e.Email1 == value.ToString());
return (Boolean) (c == 0);
}
}
This just in from asp.net forums by Brad Wilson. So pleased with it. No error handling!