Validation: using ModelState w/ Web Forms and EF

nobodyno

New Member
I'm using VS 2012/.NET 4.5, EF 5, and a solution which was imported/opened from an older VS 2012/.NET 4 project. An edmx (w/ self-tracking entities) was generated from the database in the original project and I'm simply trying to add data validation to my model, using ASP.NET web forms, NOT MVC. MVC is not an option for this project.I've tried this approach, while Googling for a couple of days and stitching various bits of information together.I've tried using MetadataType to extend my edmx model...it's just not working. Here's what it looks like:\[code\]//generated by VS 2010namespace DataAccess.Model{ [DataContract(IsReference = true)] public partial class NoBilling: IObjectWithChangeTracker, INotifyPropertyChanged { //...and so on...all generated by Visual Studio }}\[/code\]...extended:\[code\]namespace DataAccess.Model{ [MetadataType(typeof(DataAccess.Model.NoBillingMetadata))] public partial class NoBilling {} public partial class NoBillingMetadata { [Required, StringLength(50)] public string Company { get; set; } [Required, MaxLength(50)] public string EmailDomain { get; set; } }}\[/code\]...in the web form:\[code\]protected void SaveButton_Click(object sender, EventArgs e){ //assign to entity NoBilling nb = new NoBilling(); nb.Zip = "10001"; //save try { NoBillingProxy nbPx = new NoBillingProxy(); nbPx.Save(nb); } catch (Exception exp) { //data validation if (ModelState.IsValid) { //always tests true, no values in ModelState collection } throw; }}\[/code\]I know I'm missing some things here, but I don't have unlimited time to keep digging and cobbling things together. I don't know the innards of this ModelState class, how it's called, triggered, etc. I'm assuming it doesn't work because I haven't actually bound any data? I won't always bind entities to controls, so is there a way to change the "IsValid" state, sans binding?This is the article I used to get a bunch of what I tried:http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetwebforms45_topic3.aspxAny help would be appreciated, thanks.
 
Top