MVC Model require true

I would create a validator for both Server AND Client side. Using MVC and unobtrusive form validation, this can be achieved simply by doing the following: Firstly, create a class in your project to perform the server side validation like so: public class EnforceTrueAttribute : ValidationAttribute, IClientValidatable { public override bool IsValid(object value) { if … Read more

Validate object based on external factors (ie. data store uniqueness)

I would suggest an experiment that i have only been trialling for the last week or so. Based on this inspiration i am creating DTOs that validate a little differently to that of the DataAnnotations approach. Sample DTO: public class Contact : DomainBase, IModelObject { public int ID { get; set; } public string Name … Read more

Remote Validation in ASP.Net MVC 3: How to use AdditionalFields in Action Method

Strange. It works for me: Model: public class MyViewModel { [Required] [Remote(“IsEmailAvailable”, “Home”, AdditionalFields = “InitialEmail”)] public string Email { get; set; } } Controller: public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } public ActionResult IsEmailAvailable(string email, string initialEmail) { … Read more

Conditionally required property using data annotations

RequiredIf validation attribute I’ve written a RequiredIfAttribute that requires a particular property value when a different property has a certain value (what you require) or when a different property has anything but a specific value. This is the code that may help: /// <summary> /// Provides conditional validation based on related property value. /// </summary> … Read more

Custom model validation of dependent properties using Data Annotations

MVC2 comes with a sample “PropertiesMustMatchAttribute” that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this: [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = “‘{0}’ and ‘{1}’ … Read more

displayname attribute vs display attribute

DisplayName sets the DisplayName in the model metadata. For example: [DisplayName(“foo”)] public string MyProperty { get; set; } and if you use in your view the following: @Html.LabelFor(x => x.MyProperty) it would generate: <label for=”MyProperty”>foo</label> Display does the same, but also allows you to set other metadata properties such as Name, Description, … Brad Wilson … Read more

Entity Framework code first unique column

In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations.Schema; If your model field is a string, make sure it is not set to nvarchar(MAX) in SQL Server or you will see this error with Entity Framework Code First: Column ‘x’ in table … Read more