webforms - ASP.NET 4.5 - unobtrusive validation, model binding, IValidatableObject interface -
i have asp.net 4.5 webforms site, , i'm trying use new fancy features model binding , unobtrusive validation.
this works quite nicely - long validate on single fields - e.g. make sure values entered etc.
but when try use ivalidatableobject interface establish validations span multiple fields, don't expected results.
imagine have myuser class generated sql server database, ef 6.1 code-first model. extend class adding partial class implements ivalidatableobject interface:
public partial class myuser : ivalidatableobject { public ienumerable<validationresult> validate(validationcontext validationcontext) { list<validationresult> errors = new list<validationresult>(); if(username.equals(password, stringcomparison.currentcultureignorecase)) { errors.add(new validationresult("user name , password cannot same")); } return errors; } } basically want check user name , password aren't same - cannot on model class itself, therefore here.
now in update (or insert, matter) method, want check if data user has entered on web form valid:
public void update(int userid) { myuser existing = _repo.getuserbyid(userid); if (existing == null) { // handle error } tryupdatemodel(existing); if (modelstate.isvalid) { // save } } i have expected validate method on partial myuser class called check if model state valid - doesn't seem happen...
what missing? how else implement cross-field validations this?
update: problems seems validate method of ivalidatableobject interface, implemented in separate partial class file, isn't recognized. ideas why??
as move method main, ef-generated myuser.cs class, works fine. great - until day need re-create entity classes database again, when database change has been made.....
Comments
Post a Comment