dataannotations

asp.net MVC 2 Validating File uploads with data annotation

I'm trying to validate a form using Data Annotation. It seems great for string types and integers, but for a file upload, I couldn't validate from the class. It would just be sent a string "HttpPostedFileWrapper". Does anyone have any tips? Thanks ...

Validation does not work when I use Validator.TryValidateObject.

DataAnnotations does not work with buddy class. The following code always validate true. Why ? var isValid = Validator.TryValidateObject(new Customer(), Context, results, true); and here is the buddy class. public partial class Customer { public string Name { get; set; } public int Age { get; set; } } [MetadataType(typeo...

change validate message in data annotation

my object has field with data type int. when i put in html form in this textbox letter not number the validator say- The field must be a number. how can i change this messages like this [Required(ErrorMessage = "Введите название")] [DisplayName("Название")] public int age { get; set; } ...

MVC data annotation to compare one property to another?

I've been playing around data annotations in MVC2 and am curious if there is an annotation to compare 2 properties (ie. password, confirm password)? ...

DataAnnotations and Resources don't play nicely

I'm using dataannotations in an MVC2 app and am a little discouraged when trying to use RESX file resources for error messages. I've tried the following but keep getting the exception "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" [Required(ErrorMess...

asp.net MVC 1.0 and 2.0 currency model binding

I would like to create model binding functionality so a user can enter ',' '.' etc for currency values which bind to a double value of my ViewModel. I was able to do this in MVC 1.0 by creating a custom model binder, however since upgrading to MVC 2.0 this functionality no longer works. Does anyone have any ideas or better solutions fo...

MVC 2 Data annotations problem

Hi. Going mad now. I have a MVC solution that i've upgraded from MVC 1 to 2. It all works fine.... except the Validation! Here's some code: In the controller: using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Security.Principal; using System.Web; using System.Web.Mvc; using S...

Model validation with enumerable properties in Asp.net MVC2 RTM

I'm using DataAnnotations attributes to validate my model objects. My model class looks similar to this: public class MyModel { [Required] public string Title { get; set; } [Required(ErrorMessage = "At least one editor is required.")] public List<User> Editors { get; set; } } public class User { public int Id { get...

Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?

I'm using the DataAnnotations attributes along with ASP.Net MVC 2 to provide model validation for my ViewModels: public class ExamplePersonViewModel { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.Validation))] [StringLength(128, ErrorMessageResourceName = "StringLength", ErrorMessa...

DataAnnotations in ASP.NET MVC 2 - Stop MVC from applying RequiredAttribute to Non-nullable DateTime etc. properties

Im trying to create a custom version of the RequiredAttribute to replace the built in one and I've got it working for properties that have strings values, but with properties that are DateTime or integer for example, the default RequiredAttribute seems to be applied automatically (IF the property is not nullable!) My problem is that i w...

ASP.NET-MVC 2 DataAnnotations StringLength

Can I use the MVC 2 DataAnnotations to specify a minimum length for a string field? Has anyone done this or have they created custom attributes and if so do you mind sharing the source? ...

Localizing DataAnnotations Custom Validation Attribute

Hello SO, I'm currently working in an MVC 2 app which has to have everything localized in n-languages (currently 2, none of them english btw). I validate my model classes with DataAnnotations but when I wanted to validate a DateTime field found out that the DataTypeAttribute returns always true, no matter it was a valid date or not (tha...

How can I tell the Data Annotations validator to also validate complex child properties?

Can I automatically validate complex child objects when validating a parent object and include the results in the populated ICollection<ValidationResult>? If I run the following code: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace ConsoleApplication1 { public class Person ...

switching from EnterpriseLibrary.Validation to Microsoft.Practices.EnterpriseLibrary.Validation.Validators to System.ComponentModel.DataAnnotations

I'm moving some code from Microsoft.Practices.EnterpriseLibrary.Validation.Validators to System.ComponentModel.DataAnnotations, and have come across a more complex validator that I am having trouble in changing it to DataAnnotations, anyone got any pointers on how I can convert this? [NotNullValidator] [DomainValidator("M", "F", "A", Me...

MVC2 DataAnnotations validation with inheritance

Hi, I have a .NET 2.0 class the properties of which are marked virtual.I need to use the class as a model in a MVC2 application. So, I have created a .NET 3.5 class inheriting from the .NET 2.0 class and added the DataAnnotations attributes to the overriden properties in the new class. A snippet of what I have done is below // .NET 2.0 ...

Validating only selected fields using ASP.NET MVC 2 and Data Annotations

I'm using Data Annotations with ASP.NET MVC 2 as demonstrated in this post: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx Everything works fine when creating / updating an entity where all required property values are specified in the form and valid. However, what if I only want to update some o...

Displaying an asterix when using Html.LabelFor() with a Required property

I'm using ASP.NET MVC2 and Data Annotations. I've decorated a property in my buddy class with the Required attribute. Is there a way to get the Html.LabelFor() helper method to automatically display an asterix to signify that the field is required? Only ways I can think of to do this are: a) Extend LabelExtensions Or b) Manually ad...

Asp.Net MVC EnableClientValidation doesnt work.

I want as well as Client Side Validation as Server Side Validation. I realized this as the following: Model: ( The model has a DataModel(dbml) which contains the Test class ) namespace MyProject.TestProject { [MetadataType(typeof(TestMetaData))] public partial class Test { } public class TestMetaData { ...

How to use and/or localize DisplayAttribute with ASP.NET MVC2?

I am trying to figure out how to get the DisplayAttribute in my MVC 2 ViewModel to work with the Html.LabelFor() helper. Neither public class TestModel { [Display(ResourceType = typeof(Localization.Labels))] public string Text { get; set; } } nor public class TestModel { [Display(Name = "test")] public string Text { ...

How to use RegularExpression DataAnnotation with Resource file

Hi, I am currently using MVC 1.0 and .NET 3.5. I am using DataAnnotations to validate my model. I'm trying to add use the RegularExpression to validate a Postcode. I have stored my Regex in the resource file as many models will use it, when I try the following: [RegularExpression(Resources.RegexPostcode, ErrorMessage="Postcode form...