Entity Framework Code First : how to annotate a foreign key for a “Default” value?

The problem is that when you have multiple relationships between two entities, EF Code First isn’t able to find out which navigation properties match up, unless, you tell it how, here is the code: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } /****Change Nullable<int> … 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

mvc [DataType(DataType.EmailAddress) no validation

You could use the usual DataAnnotations library by just using [EmailAddress] using System.ComponentModel.DataAnnotations; [Required] [EmailAddress] public String Email { get; set; } Also just for reference, here’s the regular expression version of this validation: [RegularExpression(@”^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-‌​]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$”, ErrorMessage = “Email is not valid”)] public String Email {get; set;} Best of luck!

mvc4 data annotation compare two dates

Take a look at Fluent Validation or MVC Foolproof Validation: those can help you a lot. With Foolproof for example there is a [GreaterThan(“StartDate”)] annotation than you can use on your date property. Or if you don’t want to use other libraries, you can implement your own custom validation by implementing IValidatableObject on your model: … Read more

DataAnnotations dynamically attaching attributes

MVC has a hook to provide your own ModelValidatorProvider. By default MVC 2 uses a sub class of ModelValidatorProvider called DataAnnotationsModelValidatorProvider that is able to use System.DataAnnotations.ComponentModel.ValidationAttribute attributes for validation. The DataAnnotationsModelValidatorProvider uses reflection to find all the ValidationAttributes and simply loops through the collection to validate your models. All you need to do is … Read more

Best Practices ViewModel Validation in ASP.NET MVC

To answer your 3th question first: No there is no easier way then what you are doing. Two lines of code to get it working can hardly be easier. Although there is a plug-in you could use, like explained in the question unobtrusive validation not working with dynamic content Your first question, how to centralize … Read more

How can I use the Data Validation Attributes in C# in a non-ASP.net context?

Actually this is pretty cool. I used it in a WFP validation implementation recently. Most people end up writing lots of code using reflection to iterate the attributes, but there’s a built in function for this. var vc = new ValidationContext(myObject, null, null); return Validator.TryValidateObject(myObject, vc, null, true); You can also validate attributes on a … Read more

DataAnnotation for Required property

Okay. Though I have not complete understood this thing. A workaround is found. In Global.asax: GlobalConfiguration.Configuration.Services.RemoveAll( typeof(System.Web.Http.Validation.ModelValidatorProvider), v => v is InvalidModelValidatorProvider); I found it in the Issue Tracker in aspnetwebstack. Here is the link to the page: Overly aggressive validation for applying [DataMember(IsRequired=true)] to required properties with value types If anyone can tell us … Read more