entity framework - MVC 4 Manual validation with Data Annotation -


i admit found many similar question not :)
have entities data annotation attributes:

  public class baseentity   {     [required, datatype(datatype.datetime)]     public datetime createdat { get; set; }     public string createdby { get; set; }   }   public class exchangerights : baseentity   {     [key, databasegenerated(databasegeneratedoption.identity)]     public int id { get; set; }     [required, datatype(datatype.emailaddress)]     public string email { get; set; }   } 

and try validate empty object:

  exchangerights r = new exchangerights();   validationcontext vctx = new validationcontext(r);   list<validationresult> res = new list<validationresult>();   validator.tryvalidateobject(r, vctx, res, true); 

it's result 1 error. email field required. not indicate there not email format , not indicate createdat field not set (ok maybe it's because non nullable value type)
here twist.
first want every error should indicated it's empty, it's not email format.
bigger problem if set email "asd" method return no error.
it's wierd if use entity typed view (using mvc4 razor) on create page works fine.
form indicate email requires or if set says not valid email address format. indicates createdat field required (with or without required attribute!!) , if set it says not valid datetime format.

any help/idea/hunch appreciated.

thanks,
péter

for validation of email should use emailaddress validation attribute. [emailaddress].

for createdat, datetime non nullable type, when create exchangerights instance, property createdat populated datetime.minvalue. property has value. if want differente behavior, use datetime? instead of datetime.

hope helps.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -