data-annotations

Data Annotation with Bools

Deadly 提交于 2019-12-04 18:39:59
Down below I have three different categories. How would I structure the validation to make sure that at least one boolean gets selected per category? //Disabilities [Display(Name = "Learning Disabilities")] public bool LD { get; set; } [Display(Name = "Developmental Disabilities")] public bool DD { get; set; } [Display(Name = "AD/HD")] public bool ADHD { get; set; } [Display(Name = "Autism")] public bool Autism { get; set; } //Age Group [Display(Name = "Child")] public bool child { get; set; } [Display(Name = "Youth")] public bool youth { get; set; } [Display(Name = "Adult")] public bool adult

MVC Validation using Data Annotations - Model classes or View Model classes?

浪尽此生 提交于 2019-12-04 18:23:55
问题 Is it best practice to put data validation annotations in the Model or View Model? What are the advantages/disadvantages of one approach over the other? Curious to see where everyone is putting their validation, I am currently doing it in the model project. However I have seen a few people say this is not best practice. 回答1: As far as best practices is concerned I would say: in neither of them. Validation should be separate. Frameworks such as FluentValidation allow you to completely separate

Derived RequiredAttribute doesn't work

一世执手 提交于 2019-12-04 18:02:05
I'm trying to implement my own RequiredAttribute, in which I call a custom resource handler: public class LocalizedValidationAttributes { public class LocalizedRequiredAttribute : RequiredAttribute { private String _resourceString = String.Empty; public new String ErrorMessage { get { return _resourceString; } set { _resourceString = GetMessageFromResource(value); } } } private static String GetMessageFromResource(String resourceTag) { return ResourceManager.Current.GetResourceString(resourceTag); } } I call this the following way: [LocalizedValidationAttributes.LocalizedRequiredAttribute

The DataAnnotations [Phone] Attribute

妖精的绣舞 提交于 2019-12-04 16:22:46
问题 What is the default, valid format of the [Phone] attribute? In the data table, the phone column is navrchar (16) If I enter a phone # like 1112223333, I get "field is not a valid phone number." If I enter 01112223333, I get "The value '11112223333' is invalid." Also, how to override it? I understand that I could do something like this, but is this the best practice in this case? [RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}",ErrorMessage="Invalid Phone Number!")] Related code:

Problem with DataAnnotations in partial class

落爺英雄遲暮 提交于 2019-12-04 15:14:08
So in my mvc project's Project.Repository I have [MetadataType(typeof(FalalaMetadata))] public partial class Falala { public string Name { get; set; } public string Age { get; set; } internal sealed class FalalaMetadata { [Required(ErrorMessage="Falala requires name.")] public string Name { get; set; } [Required(ErrorMessage = "Falala requires age.")] public string Age { get; set; } } } I use Falala as a model in my Project.Web.AccountControllers, and use a method to get violations. Validating worked when I had public class Falala { [Required] public string Name { get; set; } [Required

custom unobtrusive date validators for dates

微笑、不失礼 提交于 2019-12-04 15:02:36
Maybe it's just the way my mind works, but I have a very hard time understanding how you're supposed to do custom unobtrusive validators. The C# part is easy enough, but the jqueryui adapters are where i get lost. I've been trying to make a validator that requires a date to be a certain amount of time in the past. I use this for age validation, to make sure someone has entered a date that is 18 years in the past. I finally decided to just make it a remote validator, that way the validation uses the same code both client and server side. Still, i'd be interested in the jquery to make this work.

Is there a way to reuse data annotations?

戏子无情 提交于 2019-12-04 14:44:13
Is there a way to implement the idea of a data domains (at a property level) inside of a class that is used as a model in a view in ASP.Net MVC 4? Consider this code: public class LoginProfileModel { [DisplayName("Login ID")] [Required(ErrorMessage = "Login ID is required.")] public string LogonID { get; set; } [DisplayName("Password")] [Required(ErrorMessage = "Password cannot be blank.")] [StringLength(20, MinimumLength = 3)] [DataType(DataType.Password)] public string Password { get; set; } } Here is a LoginProfileModel in for ASP.Net MVC 4. It uses a variety of metadata/data annotations so

ASP.NET MVC3 Custom Validation Message Behaviour

喜你入骨 提交于 2019-12-04 14:22:57
问题 Coming from the asp.net webforms model I'm used to using validators which display an error in the form <span title="Username is Required">*</span> . I'm quite clear how the MVC3 validators work out of the box, so please no more answers explaining how validators work in MVC3 as I'm pretty sure I have that nailed. What I am trying to accomplish is have the validation error message showing as the title of the span tag as shown in the first paragraph. I have managed to replicate this in MVC3 but

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

你说的曾经没有我的故事 提交于 2019-12-04 13:51:46
问题 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 Win Forms app. I'm now looking to do some Remote Validation using Data Annotations and have taken a look at this article: http://msdn.microsoft.com/en-us/library

using Metadata with Entity Framework to validate using Data Annotation

纵然是瞬间 提交于 2019-12-04 13:45:28
I have a entity called Product,this is a part of it's declration: [EdmEntityTypeAttribute(NamespaceName="NorthwindModel", Name="Product")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class Product : EntityObject { #region Factory Method /// <summary> /// Create a new Product object. /// </summary> /// <param name="productID">Initial value of the ProductID property.</param> /// <param name="productName">Initial value of the ProductName property.</param> /// <param name="discontinued">Initial value of the Discontinued property.</param> public static Product