How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

I appreciate the effort that went into the existing answer, but I haven’t found it actually answering the question… so I tested this, and found out that [Column(TypeName = “ntext”)] public string Body { get; set; } (the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column. (My problem with the accepted answer … Read more

The specified value does not conform to the required format yyyy-MM-dd

The specifications for the HTML5 date picker state that the date must be in the format yyyy-MM-dd (ISO format). This means that you DisplayFormatAttribute must be [DisplayFormat(DataFormatString = “{0:yyyy-MM-dd}”, ApplyFormatInEditMode = true)] public string MyDate { get; set; } Alternatively you can manually add the format using @Html.TextBoxFor(m => m.MyDate, “{0:yyyy-MM-dd}”, new { @type = … Read more

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

Issue – Model Binder Order This is, unfortunately, the standard behavior of Validator.TryValidateObject which does not recursively validate the property values of the object As pointed out in Jeff Handley’s article on Validating Object and Properties with the Validator, by default, the validator will validate in order: Property-Level Attributes Object-Level Attributes Model-Level implementation IValidatableObject The … Read more

How to create Custom Data Annotation Validators

To create a custom data annotation validator follow these gudelines: Your class has to inherit from System.ComponentModel.DataAnnotations.ValidationAttribute class. Override bool IsValid(object value) method and implement validation logic inside it. That’s it. IMPORTANT Caution Sometimes developers check that value is not null/empty and return false. This is usually incorrect behaviour, because that’s on Required validator to … Read more

Entity Framework Code First Fluent Api: Adding Indexes to columns

After Migrations was introduced in EF 4.3 you can now add indexes when modifying or creating a table. Here is an excerpt from the EF 4.3 Code-Based Migrations Walkthrough from the ADO.NET team blog namespace MigrationsCodeDemo.Migrations { using System.Data.Entity.Migrations; public partial class AddPostClass : DbMigration { public override void Up() { CreateTable( “Posts”, c => … Read more

How to retrieve Data Annotations from code? (programmatically)

Extension method: public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute { var attrType = typeof(T); var property = instance.GetType().GetProperty(propertyName); return (T)property .GetCustomAttributes(attrType, false).First(); } Code: var name = player.GetAttributeFrom<DisplayAttribute>(nameof(player.PlayerDescription)).Name; var maxLength = player.GetAttributeFrom<MaxLengthAttribute>(nameof(player.PlayerName)).Length;

Data Annotation to validate confirm password

If you are using ASP.Net MVC 3, you can use System.Web.Mvc.CompareAttribute. If you are using ASP.Net 4.5, it’s in the System.Component.DataAnnotations. [Required(ErrorMessage = “Password is required”)] [StringLength(255, ErrorMessage = “Must be between 5 and 255 characters”, MinimumLength = 5)] [DataType(DataType.Password)] public string Password { get; set; } [Required(ErrorMessage = “Confirm Password is required”)] [StringLength(255, ErrorMessage … Read more

“The Id field is required” validation message on Create; Id not set to [Required]

You can add the attribute: [Bind(Exclude = “Id”)] on the parameter in method rather than the class, that way on create you can exclude it and on edit it will still be mandatory: public ActionResult Create([Bind(Exclude = “Id”)] User u) { // will exclude for create } public ActionResult Edit(User u) { // will require … Read more