problem about dataannotations-Collection of common programming errors


  • 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 I am having some doubts.I am trying to use only attributes to accomplish this so that I don’t have some of the mapping here, some of it there, and still more of it over there….When I quer

  • Pretzel
    asp.net-mvc validation dataannotations model-binding
    Normally, 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?

  • Jordan Arron
    c# dataannotations
    Is 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?

  • Simon_Weaver
    asp.net-mvc dataannotations
    I have certain panels on my page that are hidden under certain circumstances.For instance I might have a ‘billing address’ and ‘shipping address’ and I dont want to validate ‘shipping address’ if a ‘ShippingSameAsBilling’ 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 ‘shipping address’ when it is not displayed and need to find the way way to achieve this. I am talking mainly server side

  • teresko
    asp.net-mvc dataannotations
    Is 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 “static” model[Required] [DisplayName(“Price”)] [RegularExpression(@”[0-9]+(\.[0-9][0-9]?)?$”, ErrorMessage = “Invalid price format”)] public decimal MinimumPrice { get; set; }Thanks!

  • Shaggy13spe
    vb.net winforms linq-to-sql .net-3.5 dataannotations
    Ok, I’m completely at a loss here. I’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’m on a project that I’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

  • nEEbz
    c# asp.net-mvc dataannotations
    When 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?

  • JohnnyO
    c# asp.net linq-to-sql linq-to-entities dataannotations
    I use Linq to Sql (although this is equally applicable in Entity Framework) for my models, and I’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’d like to create:[MetadataType(typeof(PersonMetadata))] public partial class Person { }public class PersonMetadata {public object Id { g

  • svick
    asp.net-mvc-2 reflection dynamic attributes dataannotations
    I have a little bit of a curve ball for you. Maybe just a design issue…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’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

  • Ronald Wildenberg
    asp.net-mvc dataannotations
    Apparently 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?

  • Eager to Learn
    c# asp.net-web-api dataannotations
    I can’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) {// …some codereturn this.Request.CreateResponse(HttpStatusCode.OK); }and the LoginType class looks like this:public class LoginModel {[Required][EmailAddress(ErrorMessage = “Please have a Email address format”)]public string Ema

  • João Angelo
    asp.net-mvc validation dataannotations
    I’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’t find a way to do this with StringLength, I will use RemoteValidator.

  • Travis J
    c# dataannotations
    Can I change the fieldpublic virtual ClassOne ClassOne { get; set; }to [ScriptIgnore] public virtual ClassOne ClassOne { get; set; }at runtime?

  • Mahmoud Moravej
    asp.net-mvc-3 dataannotations
    In 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 = “Product”)]public object Title { get; set; }}andpublic class ServiceMetaData{[Display(Name = “Service”)]public object Title { get; set; }}How can I set these classes at runtime as MetadataType?———————-

  • krlm
    c# asp.net-mvc dataannotations
    Is 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’s more proper way to do this. Thanks for any advices. Best regards

  • teresko
    c# asp.net-mvc asp.net-mvc-2 dataannotations
    I 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 = “BirthDate is required.”)] [DisplayName(“Birth Date”)] [DateOfBirthRange(ErrorMessage = “BirthDate must be between {1:M/d/yyyy}

  • H.B.
    c# silverlight silverlight-4.0 dataannotations
    Right now I am trying to show a dummy error message: “Error Message”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(“ValidationError”){Mode = BindingMode.TwoWay,NotifyOnValidationError = true,ValidatesOnExceptions = true,UpdateSourceTrigger = UpdateSourceTrigger.Explicit,Source = helper});c.GetBindingExpression(Control.TagProperty()).UpdateSource(); }My helper class looks like

  • Chandu
    c# asp.net-mvc asp.net-mvc-3 dataannotations
    I 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<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context

  • Edward Burns
    c# asp.net-mvc-2 null dataannotations nerddinner
    Platform: 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 – Section: “Integrating Validation and Business Rule Logic with Model Classes” (pages 33 to 35) ERROR Synopsis: NerdDinner form validation ERROR with DataAnnotations and db nulls. DataAnnotations

  • Display Name
    asp.net-mvc asp.net-mvc-3 jquery-validate dataannotations partialview
    I 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<string> scList = new List<string>();[Required(ErrorMessage = “SC ID

  • teresko
    jquery asp.net-mvc c#-4.0 dataannotations
    We 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

  • L.W.
    asp.net-mvc-3 validation asp.net-ajax dataannotations telerik-grid
    I 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 “undefined”, I don’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 = “This field needs a value!”)][Remote(“CheckDuplicateType”,”Type”,AdditionalFields = “applconst

  • RubbleFord
    mvc dataannotations knockout.js
    I’ve got the following code, but once it’s submitted to the server I’m getting strings of “undefined” instead of null or empty. This is causing issues as I can’t perform validation. Any ideas how to prevent this happening when using knockout.var viewModel = {userName: ko.observable(“”),emailAddress: ko.observable(“”),verifyEmailAddress: ko.observable(“”),OptOut: ko.observable(true),Grades: [“Grade 1”, “Grade 2”, “Grade 3”, “Grade 4”, “Grade 5”, “Grade 6”],gradeSelected: ko.observable([“Grade 1”]

Web site is in building