{"id":3322,"date":"2014-03-22T03:08:32","date_gmt":"2014-03-22T03:08:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/22\/problem-about-dataannotations-collection-of-common-programming-errors\/"},"modified":"2014-03-22T03:08:32","modified_gmt":"2014-03-22T03:08:32","slug":"problem-about-dataannotations-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/22\/problem-about-dataannotations-collection-of-common-programming-errors\/","title":{"rendered":"problem about dataannotations-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9bce6c85ebdefeff33828e965d95c931?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCodeWarrior<br \/>\nentity-framework-4.1 dataannotations<br \/>\nGood day,I am mapping some pre-existing Business Objects to our database using Entity Framework. These object were originally using a home-grown data access method, but we wanted to try out Entity Framework on it now that it is using Code-First. It was my expectation that this would be fairly simple, but now I am having some doubts.I am trying to use only attributes to accomplish this so that I don&#8217;t have some of the mapping here, some of it there, and still more of it over there&#8230;.When I quer<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/3b25aeb68385424102af797bac419900?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPretzel<br \/>\nasp.net-mvc validation dataannotations model-binding<br \/>\nNormally, ModelBinding Validation of a class member might be done like this example:public Class someclass {[StringLength(50)]public string SomeValue { get; set; } }SomeValue is limited to 50 characters at a maximum.Is it possible to have the constant (50) changed to something else at run-time, say, during the construction of each instance of that class, so that it is possible to have varying instances with different StringLength limitations?If so, how does one do this?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c5391165e67b5fdcb48cde12bc38106c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJordan Arron<br \/>\nc# dataannotations<br \/>\nIs there away to add and remove DataAnnotations, in particular the [requried], from the code side of things? My problem is that I want to give the user the ability to save an incomplete form in our CRUD applications but at the same time use the power of the DataAnnotations validation. If this is not possible, what is the best way I can go about this?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7fc2db839382b30434539fbc967c498e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSimon_Weaver<br \/>\nasp.net-mvc dataannotations<br \/>\nI have certain panels on my page that are hidden under certain circumstances.For instance I might have a &#8216;billing address&#8217; and &#8216;shipping address&#8217; and I dont want to validate &#8216;shipping address&#8217; if a &#8216;ShippingSameAsBilling&#8217; checkbox is checked.I am trying to use the new DataAnnotations capabilities of ASP.NET MVC 2 (preview 1) to achieve this.I need to prevent validation of the &#8216;shipping address&#8217; when it is not displayed and need to find the way way to achieve this. I am talking mainly server side<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/40de750233b3ce8268f235ba0b6a489b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nteresko<br \/>\nasp.net-mvc dataannotations<br \/>\nIs it possible to set range (or any other model DataAnnotations attributes) in code and keep all the DataAnnotations behaviour?i.e. I have a decimal model that I want to set its minimum value in code (on page load for example) in a way I could keep using the ValidationMessageFor ?my current &#8220;static&#8221; model[Required] [DisplayName(&#8220;Price&#8221;)] [RegularExpression(@&#8221;[0-9]+(\\.[0-9][0-9]?)?$&#8221;, ErrorMessage = &#8220;Invalid price format&#8221;)] public decimal MinimumPrice { get; set; }Thanks!<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/9c95991338708881f29f7a360e11b695?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nShaggy13spe<br \/>\nvb.net winforms linq-to-sql .net-3.5 dataannotations<br \/>\nOk, I&#8217;m completely at a loss here. I&#8217;ve used DataAnnotations attribute DisplayName successfully using MVC model binding, and even with WPF\/Silverlight model binding and of course it works just fine, but now I&#8217;m on a project that I&#8217;m forced to use VB.NET 3.5 WinForms. I have a Linq2Sql model and I created a partial class for one of my classes and included a MetadataType attribute to point to a metadata class. I added a DisplayName attribute to a property in the metadata class. I then bind my<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/461da8742a53e87f74227dbba016c6df?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nnEEbz<br \/>\nc# asp.net-mvc dataannotations<br \/>\nWhen I do the following:`[Range(1910, DateTime.Now.Year)] public int Year { get; set; }`I get the following error:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter typeAny ideas why?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/graph.facebook.com\/562666100\/picture?type=large\" \/><br \/>\nJohnnyO<br \/>\nc# asp.net linq-to-sql linq-to-entities dataannotations<br \/>\nI use Linq to Sql (although this is equally applicable in Entity Framework) for my models, and I&#8217;m finding myself creating buddy classes for my models all the time. I find this time consuming and repetitive. Is there an easy way to automatically generate these buddy classes based on the models? Perhaps a visual studio macro?An example of a buddy class that I&#8217;d like to create:[MetadataType(typeof(PersonMetadata))] public partial class Person { }public class PersonMetadata {public object Id { g<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0ec0f5be7749c8cc8ccda4a809103f36?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nsvick<br \/>\nasp.net-mvc-2 reflection dynamic attributes dataannotations<br \/>\nI have a little bit of a curve ball for you. Maybe just a design issue&#8230;maybe even something as simple as me not understanding Data annotation providers.Anyway here we go: I have a class which represents some model data. Let&#8217;s say it represents a package\/box\/carton.It actually represents all of these things so I use the class in several different views. Sometimes I want the attribute of the field Package_Description to be So that it shows up as Box Number : input box here.Now if i want it to<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b6edc6beebb7963bba6b902d39ba7c0f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRonald Wildenberg<br \/>\nasp.net-mvc dataannotations<br \/>\nApparently it is possible to dynamically attach DataAnnotation attributes to object properties at runtime and as such achieve dynamic validation.Can someone provide code sample on this?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/53b5338021a7f790d4e7708b491369d7?s=32&amp;d=identicon&amp;r=PG&amp;f=1\" \/><br \/>\nEager to Learn<br \/>\nc# asp.net-web-api dataannotations<br \/>\nI can&#8217;t seems to find anything related to this issue on Google. Please help !!SCENARIO: Mainly I have a WebAPI server with a controller method that expects a simple type as parameter. That API looks like this: public HttpResponseMessage Foo([FromBody] LoginModel form) {\/\/ &#8230;some codereturn this.Request.CreateResponse(HttpStatusCode.OK); }and the LoginType class looks like this:public class LoginModel {[Required][EmailAddress(ErrorMessage = &#8220;Please have a Email address format&#8221;)]public string Ema<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/6a215fd46649ac67d2b7e307a0230285?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJo\u00e3o Angelo<br \/>\nasp.net-mvc validation dataannotations<br \/>\nI&#8217;m validating a field in MVC 3 with data annotations:[StringLength(50, MinimumLength=5)] public string MyText { get; set; }Is there a way to provide a dynamic value there? Something like this:[StringLength(50, MinimumLength=GetMinimumLengthValueFromDb())] public string MyText { get; set; }My last resort is to use remote validator. If I won&#8217;t find a way to do this with StringLength, I will use RemoteValidator.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/q27Nh.png?s=32&amp;g=1\" \/><br \/>\nTravis J<br \/>\nc# dataannotations<br \/>\nCan I change the fieldpublic virtual ClassOne ClassOne { get; set; }to [ScriptIgnore] public virtual ClassOne ClassOne { get; set; }at runtime?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f30d63dad300fd89a9ec1b32e33012ab?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nMahmoud Moravej<br \/>\nasp.net-mvc-3 dataannotations<br \/>\nIn Microsoft MVC 3.0,I have a class:public class Product{public string Title {get;set;} }This class can be represent as Product or as Service , the only difference between them is just the field labels at View time.so I create two classes :public class ProductMetaData{[Display(Name = &#8220;Product&#8221;)]public object Title { get; set; }}andpublic class ServiceMetaData{[Display(Name = &#8220;Service&#8221;)]public object Title { get; set; }}How can I set these classes at runtime as MetadataType?&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/84c0065aeaa77718b0783f36e927d4b7?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nkrlm<br \/>\nc# asp.net-mvc dataannotations<br \/>\nIs it possible to apply EditableAttribute at runtime? I want to make some properties editable only when the user has some role. Unfortunately EdiatbleAttribute is sealed. I can try to apply it at runtime by reflection but maybe there&#8217;s more proper way to do this. Thanks for any advices. Best regards<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/40de750233b3ce8268f235ba0b6a489b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nteresko<br \/>\nc# asp.net-mvc asp.net-mvc-2 dataannotations<br \/>\nI have a custom data annotation validator to validate the date of birth to be between 150 years from today. Here is my custom data annotation:public class DateOfBirthRange : RangeAttribute {public DateOfBirthRange(): base(typeof(DateTime), DateTime.Now.AddYears(-150).ToShortDateString(), DateTime.Now.ToShortDateString()) { } }Using it like this:[Required(ErrorMessage = &#8220;BirthDate is required.&#8221;)] [DisplayName(&#8220;Birth Date&#8221;)] [DateOfBirthRange(ErrorMessage = &#8220;BirthDate must be between {1:M\/d\/yyyy}<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c35af79e54306caedad37141f13de30c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nH.B.<br \/>\nc# silverlight silverlight-4.0 dataannotations<br \/>\nRight now I am trying to show a dummy error message: &#8220;Error Message&#8221;I am doing this by using the following code:SetControlError(Control c,string errorMsg){Helper helper = new Helper(errorMsg);c.SetBinding(Control.TagProperty,new Binding(&#8220;ValidationError&#8221;){Mode = BindingMode.TwoWay,NotifyOnValidationError = true,ValidatesOnExceptions = true,UpdateSourceTrigger = UpdateSourceTrigger.Explicit,Source = helper});c.GetBindingExpression(Control.TagProperty()).UpdateSource(); }My helper class looks like<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1f2c107a68a8724688ec8eeb25eae263?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nChandu<br \/>\nc# asp.net-mvc asp.net-mvc-3 dataannotations<br \/>\nI am trying to use asp.net mvc 3 unobstructed javascript with jquery.I am following this Tutorial I am unclear how to do step one.I thought it was just overriding IsValid but I keep getting an error so I must be doing something wrongpublic class EmailAttribute : ValidationAttribute, IClientValidatable{public override bool IsValid(object value){return base.IsValid(value);}public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/8ddb097b348d1f2dffcbe5926a21736e?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nEdward Burns<br \/>\nc# asp.net-mvc-2 null dataannotations nerddinner<br \/>\nPlatform: Windows 7 Ultimate IDE: Visual Studio 2010 Ultimate Web Environment: ASP.NET MVC 2 Database: SQL Server 2008 R2 Express Data Access: Entity Framework 4 Form Validation: DataAnnotations Sample App: NerdDinner from Wrox Pro ASP.NET MVC 2 Book: Wrox Professional MVC 2 Problem with Chapter 1 &#8211; Section: &#8220;Integrating Validation and Business Rule Logic with Model Classes&#8221; (pages 33 to 35) ERROR Synopsis: NerdDinner form validation ERROR with DataAnnotations and db nulls. DataAnnotations<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0c29237a9f92765029b2d681d5e05515?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDisplay Name<br \/>\nasp.net-mvc asp.net-mvc-3 jquery-validate dataannotations partialview<br \/>\nI have a view Index and a Partial View SCItems. I use data annotations for validation. It works in Index, but not in my partial SCItem. I should use jquery validation? Thanks. SCModel code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.Web.WebPages.Html;namespace PunchClockMobile.Models {public class SCModel{public List&lt;string&gt; scList = new List&lt;string&gt;();[Required(ErrorMessage = &#8220;SC ID<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/40de750233b3ce8268f235ba0b6a489b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nteresko<br \/>\njquery asp.net-mvc c#-4.0 dataannotations<br \/>\nWe are trying to create some custom validation with MVC 4 data annotations, the validation we are creating is kind of message prompts more than restrictive validation. First of all we have created some custom validation classes inheriting from the ValidationAttributeClass and overriding the IsValid() method to test the data and return an ValidationResult if not valid. The view that displays this data has Partial views that use EditorTemplates to display razor generated data using our custom data<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/13abee4e9a4ff2a755086a5040c43c5b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nL.W.<br \/>\nasp.net-mvc-3 validation asp.net-ajax dataannotations telerik-grid<br \/>\nI want to check if username already exists in database using telerik mvc grid (batch edit). But The problem is in order to validate it, I need to pass applconst_id as a parameter in CheckDuplicateType function. But it always return &#8220;undefined&#8221;, I don&#8217;t know how to get the right value.this is my model class:public class masterTypeCont {[Key]public Int32 applconst_id { get; set; }[Required(ErrorMessage = &#8220;This field needs a value!&#8221;)][Remote(&#8220;CheckDuplicateType&#8221;,&#8221;Type&#8221;,AdditionalFields = &#8220;applconst<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7a68e1edee41ea3fbadc3ddaaa312b8f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRubbleFord<br \/>\nmvc dataannotations knockout.js<br \/>\nI&#8217;ve got the following code, but once it&#8217;s submitted to the server I&#8217;m getting strings of &#8220;undefined&#8221; instead of null or empty. This is causing issues as I can&#8217;t perform validation. Any ideas how to prevent this happening when using knockout.var viewModel = {userName: ko.observable(&#8220;&#8221;),emailAddress: ko.observable(&#8220;&#8221;),verifyEmailAddress: ko.observable(&#8220;&#8221;),OptOut: ko.observable(true),Grades: [&#8220;Grade 1&#8221;, &#8220;Grade 2&#8221;, &#8220;Grade 3&#8221;, &#8220;Grade 4&#8221;, &#8220;Grade 5&#8221;, &#8220;Grade 6&#8221;],gradeSelected: ko.observable([&#8220;Grade 1&#8221;]<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CodeWarrior entity-framework-4.1 dataannotations Good day,I am mapping some pre-existing Business Objects to our database using Entity Framework. These object were originally using a home-grown data access method, but we wanted to try out Entity Framework on it now that it is using Code-First. It was my expectation that this would be fairly simple, but now [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3322","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3322"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3322\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}