dataannotations

Render label for a field inside ASP.NET MVC 2 editor templates

I'm starting to use DataAnnotations in ASP.NET MVC and strongly typed template helpers. Now I have this in my views (Snippet is my custom type, Created is DateTime): <tr> <td><%= Html.LabelFor(f => Model.Snippet.Created) %>:</td> <td><%= Html.EditorFor(f => Model.Snippet.Created)%></td> </tr> The editor template for DateTime is...

Is there a good way of displaying required field indicators when using DataAnnotations in MVC 2?

I've got validation working with DataAnnotations on all my models, but I'd like to display an indicator for required fields on page load. Since I've got all my validation centralized, I'd rather not hard-code indicators in the View. Calling validation on load would show the validation summary. Has anyone found a good way of letting th...

MVC Model Validation with Data Annotations

In my model, I am apply Data Annotations that takes multiple values. I can get it to work at the class level but the values are not passed with I try this at the property level. My goal is to add the error at the property level, not form level. [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "...")] public class Reg...

Data Annotations for validation, at least one required field?

If I have a search object with a list of fields, can I, using the System.ComponentModel.DataAnnotations namespace, set it up to validate that at least one of the fields in the search is not null or empty? i.e All the fields are optional but at least one should always be entered. ...

Is it possible to use Data Annotations to validate parameters passed to an Action method of a Controller?

I am using Data Annotations to validate my Model in ASP.NET MVC. This works well for action methods that has complex parameters e.g, public class Params { [Required] string Param1 {get; set;} [StringLength(50)] string Param2 {get; set;} } ActionResult MyAction(Params params) { If(ModeState.IsValid) { ...

ASP.NET MVC 2 Display Name DataAnnotaion from Resource not working

Hi, I'm trying to use a resource file to hold label text for the Model in an MVC 2 project. I've got the following class... public class Person { [Display(ResourceType = typeof(Resources.Labels),Name="First")] public string FirstName { get; set; } public string LastName { get; set; } } ...and have tried using... <...

Automatically generate buddy classes from model in C#

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 examp...

DataAnnotations validation from a class

I am using DataAnnotations in a project that is a pure C# application, what is the best way to validate my models/documents against the DataAnnotations attributes? ...

Applying data annotation on entity framework auto generated file

I have generated entity framework designer classes . After Generating the designer what is the most nicest and cleanest way to apply data annotation to the properties there . I have 3 classes there ...

Where are the Entity Framework t4 templates for Data Annotations?

I have been googling this non stop for 2 days now and can't find a single complete, ready to use, fully implemented t4 template that generates DataAnnotations. Do they even exist? I generate POCOs with the standard t4 templates. The actual database table has metadata that describes some of the validation rules, eg not null, nvarchar(2...

C# ASPNET MVC - How do you use ModelState.IsValid in a jquery/ajax postback?

From what I've seen ModelState.IsValid is only calculated by the MVC frame work on a full postback, is that true? I have a jquery postback like so: var url = "/path/to/controller/myaction"; var id = $("#Id").val(); var somedata = $("#somedata").val(); // repeated for every textbox $.post(url, { id: id, somedata: somedata }, function (...

Best Practices - Data Annotations vs OnChanging in Entity Framework 4

I was wondering what the general recommendation is for Entity Framework in terms of data validation. I am relatively new to EF, but it appears there are two main approaches to data validation. The first is to create a partial class for the model, and then perform data validations and update a collection of rule violations. This is outli...

ASPNET MVC - Override Html.TextBoxFor(model.property) with a new helper with same signature?

I want to override Html.TextBoxFor() with my own helper that has the exact same signature (but a different namespace of course) - is this possible, and if so, how? The reason for this is that I have 100+ views in an already existing app, and I want to change the behaviour of TextBoxFor so that it outputs a maxLength=n attribute if the p...

Can I get RangeAttribute functionality in my own code?

A bunch of cool attributes were introduced in .NET 3.5 SP1 to help with property validation. They are in the System.ComponentModel.DataAnnotations namespace and can be used like this: public class Person { [Range(0, 120)] public int Age { get; set; } } Technologies like Dynamic Data, MVC, and Silverlight automatically use the attr...

MVC 2 Validation and Entity framework

I have searched like a fool but does not get much smarter for it.. In my project I use Entity Framework 4 and own PoCo classes and I want to use DataAnnotations for validation. No problem there, is how much any time on the Internet about how I do it. However, I feel that it´s best to have my validation in ViewModels instead and not let ...

Custom DataAnnotations with RIA Services

This is a word of warning more than a question, if you are using RIAServices with Custom ValidatorAttributes. Here is the Senario, I was creating a custom DataAnnotation that would validate a property based on whether or not a possible series of other properties had been set, such as; if Prop1 was 100 then Prop2, Prop3, or Prop4 could n...

Using Data Annotations on POCO's with MVC for Remote Validation

Hi All, I am developing an ASP.NET MVC app and I've been looking into using Data Annotations on my POCO's which are defined in my Service Layer. As long as I have a reference to System.ComponentModel & System.ComponentModel.DataAnnotations this is no problem and what I like about this is that it allows me to reuse my Service Layer in a ...

Error messages for model validation using data annotations

Given the follow classes: using System.ComponentModel.DataAnnotations; public class Book{ public Contact PrimaryContact{get; set;} public Contact SecondaryContact{get; set;} [Required(ErrorMessage="Book name is required")] public string Name{get; set;} } public class Contact{ [Required(ErrorMessage="Name is required")]...

ASP.NET MVC2 DataAnnotations Minimum length

Because I'm not using .net 4 I can't use StringLength.MinimumLength property. What is alternative? I suppose I should write regular expression: [Required] [RegularExpression("", ErrorMessage = "Minimum 3 characters")] public string Password { get; set; } Thanks, Ile ...

What version of DataAnnotations is included with ASP.NET MVC 2 RTM

I'm working on a project using Visual Studio 2008 and have moved from the the MVC 2 Preview to RTM version. We would like to use model validation such as: public class ViewModel { [Required(ErrorMessage="UserName is required.")] [StringLength(10, ErrorMessage="UserName cannot be greater than 10 chars.")] public string UserN...