dataannotations

ASP.NET MVC: DataAnnotations - Show an error message indicating that a field must be numeric

There appears to be something of a hole in the way DataAnnotations works in that a user entering in some text into a field that will go into an int will never reach the DataAnnotations code. It kicks off a model binding error and displays the error to the user "The value 'a' is not valid for the XXXX field." Anyway, it's all very nice t...

Validating DataAnnotations with Validator class

I'm trying to validate a class decorated with dataannotation with the Validator class. It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code.. this works: public class Pers...

How would I validate string length using DataAnnotations in asp.net mvc?

I am using DataAnnotations in an ASP.NET MVC 1 application to check for Required fields and numerical ranges using the Required and Range attributes. I am looking for the best way to validate the length of strings in a few input text boxes. I see that there is a RegularExpression attribute that could do the job but I was wondering if ...

asp.net mvc multiselect remember state after postback

Hi, I am using the DataAnnotations for error checking on my asp.net mvc app, I am also using strongly typed ViewModels too. My error checking is working fine and is posting back to my view with error messages if a field is blank. However i have a MultiSelect / Listbox on my form which i need to remember it's state after an error. At t...

MVC 2 EditorForModel() rendering foreign key table names

I have an interesting problem and wanted so see if anyone else has seen this. I've created a MVC 2 site using Visual studio 2010 beta 2. I'm using linq to sql data model objects with data annotations. In my data model objects I'm using [ScaffoldColumn(false)] attribute to exclude the foreign key ID's from rendering to the UI when I use...

null values from listbox, are not evaluated in the model binding of ASP.NET-MVC

The model validation doesn't evaluates the attributes linked to listbox values if you don't select at least one of them. This way is not possible to do a model evaluation using DataAnnotations in order to inform required values. The controller: using System; using System.Collections.Generic; using System.Linq; using System.Web; using S...

.Net - DataAnnotations - Validate 2 dependants DateTime

Let say I got the following classes : public class Post { public Date BeginDate { get; set; } [Validate2Date(BeginDate, EndDate, ErrorMessage = "End date have to occurs after Begin Date")] public Date EndDate { get; set; } } public class Validate2Dates : ValidationAttribute { public Validate2Dates(DateTime a, DateTime...

DataAnnotations vs IDataErrorInfo

DataAnnotations vs IDataErrorInfo Pros and Cons of both? Benefits of one over the other? (especially related to MVC) ...

Problem with DataAnnotations in partial class

So in my mvc project's Project.Repository I have [MetadataType(typeof(FalalaMetadata))] public partial class Falala { public string Name { get; set; } public string Age { get; set; } internal sealed class FalalaMetadata { [Required(ErrorMessage="Falala requires name.")] public string Name { get; set; } ...

Using DataAnnotations on Windows Forms project

I recently used ASP.Net MVC with DataAnnotations and was thinking of using the same approach for a Forms project but I'm not sure how to go about it. I have set my attributes but they do not seem to get checked when I click Save. UPDATE: I have used Steve Sanderson's approach which will check for attributes on my class and return a col...

How to validate Data Annotations with a MetaData class

I'm trying to validate a class using Data Annotations but with a metadata class. [MetadataType(typeof(TestMetaData))] public class Test { public string Prop { get; set; } internal class TestMetaData { [Required] public string Prop { get; set; } } } [Test] [ExpectedException(typeof(ValidationException))...

Get encompassing class, then access a property of it inside custom attribute

I'm using ASP.NET MVC and implementing custom validation via custom attributes/data annotations on my models. Is it possible to access a property on an object's parent class inside my custom attribute? public class MyModel { [MyCustomValidator] public string var1 {get; set;} public string var2 {get; set;} } Note: Using as...

Data Annotation/Validation and dynamic values

If some of my models have dynamic validation conditions (i.e. the string length can be minimum of 8 or 12 depending on a database value or some other dynamic value) is it impossible to use data annotation for validation? From what I understand, the values of any parameter (example StringLength min/max value) have to be truly static. Are...

ASP.NET MVC - Data Annotations - Why add a default RequiredAttribute?

Can anyone explain why it is assumed that a non nullable type property should always have a RequiredAttribue? I am trying to write a label helper that will auto append * or change the css class so that I can indicate to the user that the field is required. However when querying the metadata the non nullable properties end up with a req...

ASP.Net MVC 2 RC: How To Use Client Validation with Data Annotations for Lists?

My problem: I can't get Data Annotations Client Validation to work with a List in my viewdata class. The skinny: In my view data class I have a List. public class FriendsViewData { public List<Person> people { get; set; } } I have all the properties of the class Person as required using Data Annotations. public class Person ...

Asp MVC - "The Id field is required" validation message on Create; Id not set to [Required]

This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2. The POCO has the following properties: public int Id {get;set;} [Required] public string Message {get; set} On the creation of the entity, the Id is set automatically, so there is no need for it on the Create action. The ModelState says ...

Unit Testing ASP.NET DataAnnotations validation

I am using DataAnnotations for my model validation i.e. [Required(ErrorMessage="Please enter a name")] public string Name { get; set; } In my controller I am checking the value of ModelState. This is correctly returning false for invalid model data posted from my view. However, when executing the unit test of my controller ac...

Model-level validation

How does one accomplish "model-level" validation as stated in Brad Wilson's post: Finally, if you want a validation to have access to multiple properties, then make it a model-level validation (so that it gets the entire model as the model parameter, rather than a single individual property value). From http://forums.asp.net/p/1457...

Selective client side validation in ASP.NET MVC

How can I implement selective client side validation using MVC 2 built-in validation system? Assume I have a checkbox in my form title "Do you have any child?" and if checked the textbox below it should be required (textbox titled Number of children). ...

How to replace standard DataAnnotations error messages

Hello. I'm using System.ComponontModel.DataAnnotations to validate my model objects. How could I replace messages standard attributes (Required and StringLength) produce without providing ErrorMessage attribute to each of them or sub classing them? ...