validation - Validating uniqueness with data annotations in asp.net mvc -


i have various questions validation using data annotations. using following setup

asp.net mvc 2 entity framework 4 data annotations

basically, i'm trying unique validation working , i'm little confused. models follows:

    public class buyer {     public int id { get; set; }      [required(errormessage = "the email required")]     public string email { get; set; }      [required(errormessage= "the name required")]     public string name { get; set; } }      public class seller {     public int id { get; set; }      [required(errormessage = "the email required")]     public string email { get; set; }      [required(errormessage= "the name required")]     public string name { get; set; } } 

i have set unique field attribute follows

public class uniquefieldattribute : validationattribute {     public iuniquevalidator validator { get; set; }     public int id { get; set; }      public override bool isvalid(object value)     {         if (value == null)         {             return true;         }          return validator.isvalid(convert.tostring(value), id);     }             } 

i created validator implements iuniquevalidator interface:

public class buyeruniqueemailvalidator : iuniquevalidator {     public bool isvalid(string value, int id)     {         thedb db = new thedb();           var existing = buyer b in db.buyers                        b.email.tolower() == value.tolower()                        select b;          foreach (buyer b in existing)         {             if (b.id != id)             {                 return false;             }         }          return true;     } } 

the idea there! however, on execution having problems. when add this

[uniquefield(validator=new buyeruniqueemailvalidator(), id=this.id errormessage= "this email in use")] 

the project won't compile.

basically, want know if possible pass class validationattribute perform validation? also, how can pass id.

additionally, there anyway create generic unique field generator work models have email field, or have have buyeremailvalidator, selleremailvalidator etc, etc. can't seem t working correctly.

only worried serverside @ moment.

thanks

your code implementation of can't see records not visible own transaction, and, hence, can't work reliably in multi-user environment. why not use db constraint?


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -