dataannotations

When using Data Annotations with MVC, Pro and Cons of using an interface vs. a MetadataType

If you read this article on Validation with the Data Annotation Validators, it shows that you can use the MetadataType attribute to add validation attributes to properties on partial classes. You use this when working with ORMs like LINQ to SQL, Entity Framework, or Subsonic. Then you can use the "automagic" client and server side vali...

How to enforce unique-field validation in MVC

I am in the way building some MVC Application and I really love the Data Annotations support in MVC. The build in support is good enough to enforce simple validation checkup. I wonder, how to implement unique-field validation using custom data-annotation ? For example, I have a view model that need the user to register a new login name, ...

Dynamic localization with Data Annotations possible?

Hi, I'm trying to dynamicly update the language of a Silverlight application. I tried the example provided by Tim Heuer and that was exactly wat I needed. Silverlight and localizing string data Now I'm experimenting with Data Annotations and would like to have the same behaviour.But with no luck... Can someone point me in the right d...

Custom DataAnnotation attribute with datastore access in ASP.NET MVC 2

I have my application designed with Repository pattern implemented and my code prepared for optional dependency injection in future, if we need to support another datastore. I want to create a custom validation attribute for my content objects. This attribute should perform some kind of datastore lookup. For instance, I need my content ...

How can I support conditional validation of model properties

I currently have a form that I am building that needs to support two different versions. Each version might use a different subset of form fields. I have to do this to support two different clients, but I don't want to have entirely different controller actions for both. So, I am trying to come up with a way to use a strongly typed mo...

ASP.NET MVC does not add ModelError when invoking from unit test

I have a model item public class EntryInputModel { ... [Required(ErrorMessage = "Description is required.", AllowEmptyStrings = false)] public virtual string Description { get; set; } } and a controller action public ActionResult Add([Bind(Exclude = "Id")] EntryInputModel newEntry) { if (ModelState.IsValid) { ...

MVC2 DataAnnotations with Server-side validation

Hi, How do you validate an entity containing DataAnnotations without using the MVC library? Using Model.IsValid is fine when you're within the Presentation layer, but what about when you want to ensure the model is valid in the Domain/Business layer? Do I need a separate validation framework, or is there an easy way I'm missing? Thank...

How can I use a custom ValidationAttribute to ensure two properties match?

We're using xVal and the standard DataAnnotationsValidationRunner described here to collect validation errors from our domain objects and view models in ASP.NET MVC. I'd like to have a way to have that validation runner identify when two properties don't match through the use of custom DataAnnotations. Right now I'm forced into doing i...

List<string> PropertyName isn't showing up in modelState.errors

Imagine, a model like this: [AddressValidation] public AddressType[] Address { get; set; } internal class AddressValidation : ValidationAttribute { public override bool IsValid(object value) { //Assume we are valid var isValid = true; //Cast to something useful var addresses = (AddressType[]...

ASP.NET MVC 2 client side validation not working for Html.ValidationMessage()?

I'm trying to get a very simple client side validation example to work in ASP.NET MVC 2. I'm using data annotations to set a required property "Surname". When I use the Html.ValidationMessageFor(x => x.Surname) the correct client side validation script is written to the page. But when I use Html.ValidationMessage("Surname") the client si...

Custom ValidationAttribute test against whole model

I know this is probably not possible but let's say I have a model with two properties. I write a ValidationAttribute for one of the properties. Can that VA look at the other property and make a decision? So; public class QuickQuote { public String state { get; set; } [MyRequiredValidator(ErrorMessage = "Error msg")] publ...

MVC2 Annotations dll not found by VS2008 compiler, how do I make it look in the right place?

I have VS2008 SP1 running with .NET 3.5 SP1 I have MVC2 running (with MVC1 uninstalled) All works ok except when I call the System.Components.DatAnnotations.dll, then the compiler complains of not being able to find it. Message is: Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=99.0.0.0, Culture=ne...

ASP.NET MVC DisplayAttribute and interfaces

I have some interface and classes public inteface IRole { int Id { get; } string Name { get; set; } } public class Role : IRole { public int Id { get; } [Display("Role Name")] public string Name { get; set; } } public class Member { [Display("Login")] public string Login { get; set; } [Display("Password")] public st...

ASP.NET MVC2 DataAnnotations not catching error

Can somebody help me to figure out why DataAnnotations will not work with my MVC2 project in VS 2008 SP1? Here's the situation.. I uninstalled VS2008 and MVC1, then reinstalled VS2008 SP1 and .NET 3.5 SP1 and MVC2. Now when I create a clean project as soon as it has to hit the DataAnnotations Dll (e.g. say when I go to Register.aspx i...

How to dynamically set validation messages on properties with a class validation attribute

I'm writing Validation attribute that sits on the class but inspects the properties of the class. I want it to set a validation message on each of the properties it finds to be invalid. How do I do this? This is what I have got so far: [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] public class Li...

SelfValidation in DataAnnotations?

With Validation Application block, there's the following functionality: Creating Custom attributes Creating SelfValidation on the type Ability to read from external config file I plan to use the DataAnnotations to replace the Validation application block. Are the above possible with DataAnnotations? If so, how'd I implement them? An...

ASP.NET MVC 1.0 Intellisense in Visual Studio 2010

I just upgraded a .NET 3.5/ASP.NET MVC 1.0 project to Visual Studio 2010. The upgrade utility tried to switch all of my references to ASP.NET MVC 2.0, which is not what I want to do at this point, so I had to change several project references and the web.config file back to use ASP.NET MVC 1.0. Right now, I have the solution building a...

Why can't I use resources as ErrorMessage with DataAnnotations?

Why can't I do like this? [Required(ErrorMessage = "*")] [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)] public string Username { get; set; } What is the error message telling me? An attribute argument must be a constant expression , typeof expression or array creation express...

Custom DataAnnotations Validator Derived from RegularExpressionAttribute

The Gu provides an example of how you might create a custom validator that overrides RegularExpressionAttribute (http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx). The advantage of this is that you don't have to create a custom Model Validator (http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-...

How can I create a generic UniqueValidationAttribute in C# and DataAnnotation?

I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be generic as in I could pass the Linq DataContext, the table name, the field and validate if the incoming value is unique or not. Here is a non-compilable code fragment that I'm stuck at right now: using System; u...