Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation: Using ModelState w/ Web Forms and EF
    primarykey
    data
    text
    <p>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.</p> <p>I've tried this approach, while Googling for a couple of days and stitching various bits of information together.</p> <p>I've tried using MetadataType to extend my edmx model...it's just not working. Here's what it looks like:</p> <pre><code>//generated by VS 2010 namespace DataAccess.Model { [DataContract(IsReference = true)] public partial class NoBilling: IObjectWithChangeTracker, INotifyPropertyChanged { //...and so on...all generated by Visual Studio } } </code></pre> <p>...extended:</p> <pre><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></pre> <p>...in the web form:</p> <pre><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></pre> <p>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?</p> <p>This is the article I used to get a bunch of what I tried:</p> <p><a href="http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetwebforms45_topic3.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/vs11trainingcourse_aspnetwebforms45_topic3.aspx</a></p> <p>Any help would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload