dataannotations

Why use primary keys?

What are primary keys used aside from identifying a unique column in a table? Couldn't this be done by simply using an autoincrement constraint on a column? I understand that PK and FK are used to relate different tables, but can't this be done by just using join? Basically what is the database doing to improve performance when joini...

Custom Model-Binder that pulls from a cookie problem?

I am trying to do the following. Use the default model binder to bind an object from query string values. If that fails, I then try and bind the object from cookie values. However I am using dataannotations on this object and I am having the following problems. If there are no querystring parameters the default model binder doesn't...

asp.NET MVC 2 DataAnnotations UpdateModel<T> validation

I'm trying to use DataAnnotations to add validation to my models in asp.NET MVC 2 RC2, using TryUpdateModel var user = UserManager.Find(id); this.TryUpdateModel<IProvisioningObject>(user, form.ToValueProvider()); This updates the model, but the validation is never called. I tried using TryUpdateModel as well (which is...

How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?

I've got a view model like this: public class SignUpViewModel { [Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")] [DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")] public bool AgreesWithTerms { get; set; } } The view markup code: <%= Html.CheckBoxFor(m => m.AgreesWithTerms) %> <%= Html.La...

Data Annotation Ranges of UK Datetime fields

I want to use Data Annotations to validate DateTime fields, but I'm running into problems. According to documentation on MSDN, the following should do the job [Range(typeof(DateTime), "1/2/2004", "3/4/2004", ErrorMessage = "Value for {0} must be between {1} and {2}")] However, this marks any date I enter as invalid! At first I th...

Should validation rules always be enforced at the model level?

With the new ASP.NET MVC 2 validation features, we can decorate the properties of our domain model objects with DataAnnotations attributes that describe criteria for valid values. The DefaultModelBinder knows about this and populates ModelState.IsValid accordingly before a controller action is invoked. Since the validation rules are defi...

ASP.NET MVC 2 Data annotations in a dynamically generated model

Hello everyone, I am creating an asp.net mvc 2 application generating my view model dynamically depending on user input. Simply put, the user is able to choose which fields he wants to see in his View. Since the templated helpers rely heavily on model properties and attributes (data annotations), I would need to somehow add the attribu...

ASP.NET ViewModel Alternative

In Django/Python, if I had the following model class Model1: id = char field name = char field creation_time = datetime field I can create a form (view model) like the following class Model1Form(Model1): exclude = {'id', 'creation_time'} I then pass it into a view/template and it would ignore id/creation_time In...

Linq to Entity Model's DataAnnotations don't reset

In my Asp.net MVC app, I have a custom validator class V and an (ADO.NET Entities) entity model E. class V : ValidationAttribute { public override bool IsValid(object value) { ... if (hasErrors) ErrorMessage = errorMsg; ... } } public partial class E //the entity m...

Custom model validation of dependent properties using Data Annotations

Since now I've used the excellent FluentValidation library to validate my model classes. In web applications I use it in conjunction with the jquery.validate plugin to perform client side validation as well. One drawback is that much of the validation logic is repeated on the client side and is no longer centralized at a single place. ...

Validate MVC 2 form using Data annotations and Linq-to-SQL, before the model binder kicks in (with DateTime)

I'm using linq to SQL and MVC2 with data annotations and I'm having some problems on validation of some types. For example: [DisplayName("Geplande sessies")] [PositiefGeheelGetal(ErrorMessage = "Ongeldige ingave. Positief geheel getal verwacht")] public string Proj_GeplandeSessies { get; set; } This is an integer, and I'm validating ...

Is there any way to stop DataAnnotation validation after the first failure?

In my ViewModels I use several DataAnnotations to validate the form data, there are usually 2-3 annotations per field. For example a field for an email address might look like this: [Required(ErrorMessage = "Please enter an email address.")] [Email(ErrorMessage = "That is not a valid email address.")] // Custom public string Email { ge...

mvc 2.0 validation

I am use DataAnnotations validation, it work perfectly but when I validate empty text box field I have error The value '' is invalid how can I customize this error? p.s. error shows only when clients script are off ...

Generating Data Annotations from Generated Classes

I have a linq to sql object or if neccessary Entity Framework object. I want to do MVC 2 Data Annotations for them, but I am endlessly lazy. Is there a way to automatically generate the data annotations a-la [Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")] [MetadataType(typeof(Dinner_Val...

Is it possible to use data annotations for LabelFor,ValidationMessageFor, EditorFor with strongly typed resources?

I would like to use DataAnnotations in my ASP.NET MVC application. I have strongly typed resources class and would like to define in my view models: [DisplayName(CTRes.UserName)] string Username; CTRes is my resource, automatically generated class. Above definition is not allowed. Are there any other solutions? ...

How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

I'm building a MVC web application with C#. Since the site will be multilingual, I've implemented my own ResourceManager. This class is responsible for fetching the required resource strings from a database/cache depending on the currents thread culture and works fine so far. My problem is, I'd like to use the my custom ResourceManager...

DisplayFor ignores metadata

For my Contact class, the property EmailAddress is marked with the [DataType(DataType.EmailAddress)] attribute. In my view, using Html.Display("EmailAddress") and Html.DisplayFor(c => c.EmailAddress) yields different results. The former outputs a mailto: link, which is the expected behavior, while the latter simply outputs the email add...

How do you get model metadata from inside a ValidationAttribute?

MVC2 comes with a nice sample of a validation attribute called "PropertiesMustMatchAttribute" that will compare two fields to see if they match. Use of that attribute looks like this: [PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] public class ChangePass...

Validating disconnected POCOs

In my ASP.NET application I have separate projects for the Data, Business and UI layers. My business layer is composed of plain objects with declarative validation, using DataAnnotations. Problem is, when it comes to save them, I'm not sure how to process the validation, since they're not bound directly to any data context, but rather,...

Is the DataTypeAttribute validation working in MVC2?

As far as I know the System.ComponentModel.DataAnnotations.DataTypeAttribute not works in model validation in MVC v1. For example, public class Model { [DataType("EmailAddress")] public string Email {get; set;} } In the codes above, the Email property will not be validated in MVC v1. Is it working in MVC v2? Thanks in advance. ...