data-annotations

How to create Custom Data Annotation Validators

耗尽温柔 提交于 2019-11-26 17:29:51
Wanting to create custom data annotation validation. Are there any useful guides / samples on how to create them? Firstly: StringLength with minimum and maximum length. I'm aware .NET 4 can do this, but want to do the same in .NET 3.5, if possible being able to define minimum length only (at least x chars), maximum length only (up to x chars), or both (between x and y chars). Secondly: Validation using modulus arithmetic - if the number is a valid length, I wish to validate using the Modulus 11 algorithm (I have already implemented it in JavaScript, so I guess it would just be a simple porting

How can I specify the order of DataAnnotation ValidationAttribute's?

元气小坏坏 提交于 2019-11-26 17:07:32
问题 The question here is similar, but I don't have any domain object inheritance. My field and validation tags are in the following order, but the MustBe18 error and the Required error are the only ones that print. I have several other fields in this model with much more validation, but the order of ValidationAttribute's in the code doesn't seem to matter. jfar's answer in the linked post seems to suggest a helper could be built, but how? How can the order be controlled? [Required(ErrorMessage =

Using DataAnnotations to compare two model properties

拈花ヽ惹草 提交于 2019-11-26 16:31:19
问题 How would I go about writing a custom ValidationAttribute that compares two fields? This is the common "enter password", "confirm password" scenario. I need to be sure the two fields are equal and to keep things consistent, I want to implement the validation via DataAnnotations. So in pseudo-code, I'm looking for a way to implement something like the following: public class SignUpModel { [Required] [Display(Name = "Password")] public string Password { get; set; } [Required] [Display(Name =

Foreign keys in entity framework 4.1

*爱你&永不变心* 提交于 2019-11-26 16:27:04
问题 I am working on Entity Framework 4.1 and using data annotations for foreign keys. I want to know how can we define one to many relationship between product and categories. I want to map category. categoryId with product.cid public class Category { public string CategoryId { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } public class Product { public int ProductId { get; set; } public string Name { get; set; } public string CId { get;

ASP.Net MVC: Can you use Data Annotations / Validation with an AJAX / jQuery call?

﹥>﹥吖頭↗ 提交于 2019-11-26 16:19:03
问题 Can you use Data Annotations / Validation with an AJAX / jQuery call? If so, please provide an example or a post which shows an example. Basically I have seen an example of how to use Data Annotations, however it was with a full post back. Is there a way to do with an AJAX / jQuery call? Not sure how you would do this since I am not sure how you would construct the Model object on the client side. (I assume this is what you would have to do.) Someone told me this can be done, but I just don't

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

狂风中的少年 提交于 2019-11-26 15:55:48
问题 I have 2 classes: Client and Survey. Each Client can have many surveys - but only one default survey. I have defined the classes like this: public class Client { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } public string ClientName { get; set; } public Nullable<int> DefaultSurveyID { get; set; } [ForeignKey("DefaultSurveyID")] public virtual Survey DefaultSurvey { get; set; } public virtual ICollection<Survey> Surveys { get; set; } } public class Survey {

What does it mean for a property to be [Required] and nullable?

六月ゝ 毕业季﹏ 提交于 2019-11-26 15:32:03
What does it mean for a property to be [Required] and nullable? (example below) It seems that if it is [Required] it couldn't possibly be null (no value), and if it is able to be null it couldn't possibly be [Required] . [Required] public DateTime? OrderDate { get; set; } The reason for making a property nullable and marked with the [Required] attribute is to protect against under-posting attacks. It also allows you to display an initial empty value in the view rather than the default value for the property. This is typically done with value type properties in view models. An under-posting

Best Practices ViewModel Validation in ASP.NET MVC

假装没事ソ 提交于 2019-11-26 15:20:52
问题 I am using DataAnnotations to validate my ViewModel on client side with jquery.validate.unobtrusive and on server side in ASP.NET MVC application. Not so long time ago, I figured out that I can write validation like this: [Required(ErrorMessage = "{0} is required")] public string Name { get; set; } That way I can easily define some general strings in config or in resources and always use it in DataAnnotations . So it will be easier to change validation messages in my whole application in

Unit Testing ASP.NET DataAnnotations validation

无人久伴 提交于 2019-11-26 15:18:26
问题 I am using DataAnnotations for my model validation i.e. [Required(ErrorMessage="Please enter a name")] public string Name { get; set; } In my controller I am checking the value of ModelState. This is correctly returning false for invalid model data posted from my view. However, when executing the unit test of my controller action, ModelState always returns true: [TestMethod] public void Submitting_Empty_Shipping_Details_Displays_Default_View_With_Error() { // Arrange CartController controller

MetadataType problem

有些话、适合烂在心里 提交于 2019-11-26 14:39:33
问题 I'm using VS2008 SP1, WCF Ria Service July 2009 CTP. I found out that MetadataType does not work in partial class mode, really don't know what I have missed out: Work:- public partial class Person { private string _Name; [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")] [StringLength(3)] public string Name { set{_Name = value;} get{return _Name;} } } class Program { static void Main(string[] args) { Person p = new Person { Name="123432" }; List res = new List();