data-annotations

When using Data Annotations with MVC, Pro and Cons of using an interface vs. a MetadataType

心已入冬 提交于 2019-12-18 12:32:24
问题 If you read this article on Validation with the Data Annotation Validators, it shows that you can use the MetadataType attribute to add validation attributes to properties on partial classes. You use this when working with ORMs like LINQ to SQL, Entity Framework, or Subsonic. Then you can use the "automagic" client and server side validation. It plays very nicely with MVC. However, a colleague of mine used an interface to accomplish exactly the same result. it looks almost exactly the same,

Validator.TryValidateObject Not Validating RangeAttribute

断了今生、忘了曾经 提交于 2019-12-18 12:03:52
问题 Given the following object, public class Question { [Required] public string QuestionText { get; set; } [Range(1, 5)] public int Difficulty { get; set; } } With the following Validation Code ICollection<ValidationResult> results = new List<ValidationResult>(); Question question = new Question(); ValidationContext ctx = new ValidationContext(question, null, null); Validator.TryValidateObject(question, ctx, results); // results.Length = 1 Why does Range attribute not create a validation error

Integer validation against non-required attributes in MVC

强颜欢笑 提交于 2019-12-18 08:48:20
问题 I've trying to validate a property on a model I have. This property is NOT required, and so if its invalid MVC seems to be ignoring it. I've even created a custom ValidationAttribute, but nothing works. public class NumberWang : ValidationAttribute { public override bool IsValid(object value) { if (value == null) return true; int g; if (int.TryParse(value.ToString(), out g)) { if (g >= 0) return true; } return false; } } public class MyModel { [Range(0, 999999, ErrorMessage = "category_id

Possible to change Data Annotations during Runtime? (ASP.NET MVC's [Range] [Required] [StringLength] etc.)

匆匆过客 提交于 2019-12-18 06:26:32
问题 Normally, ModelBinding Validation of a class member might be done like this example: public Class someclass { [StringLength(50)] public string SomeValue { get; set; } } SomeValue is limited to 50 characters at a maximum. Is it possible to have the constant (50) changed to something else at run-time, say, during the construction of each instance of that class, so that it is possible to have varying instances with different StringLength limitations? If so, how does one do this? 回答1: Yes. But

Required attribute for an integer value

流过昼夜 提交于 2019-12-17 23:26:03
问题 I have a viewmodel with an Id property [Required] public int Id { get; set; } But I think this attribute is working only for string properties. When no Id is set, Id has value 0 and the model is valid. How can I enforce that if no value for a int property is set, the model will be invalid ? 回答1: Change the type to Nullable<int> (shortcut int? ) to allow null values. 回答2: Use the Range Attribute. Set minimum to 1 and maximum to int.MaxValue [Range(1, int.MaxValue, ErrorMessage = "Value for {0}

Asp.Net Mvc Hidden Field from Data Annotations

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 21:57:34
问题 I thought this would be a quick search on google but maybe I'm missing something. Is there a way, using Data Annotations, to set a ViewModel property to create a HiddenInput when the markup get rendered? The only annotations I've found were to hide the property from the view entirely, I still want the property rendered but as a hidden input. 回答1: This property: [System.Web.Mvc.HiddenInput(DisplayValue = false)] public int Id { get; set; } will be rendered as i.e. <input id="Id" name="Id" type

ASP.NET MVC: Is Data Annotation Validation Enough?

我的梦境 提交于 2019-12-17 21:53:56
问题 I'm using the Data Annotation validation extensively in ASP.NET MVC 2. This new feature has been a huge time saver, as I'm now able to define both client-side validation and server-side validation in one place. However, while I was doing some detailed testing, I realized that it's quite easy for someone to bypass the server-side validation if I relied on Data Annotation validation alone. For example, if I defined a required field by annotating the property with the [Required] attribute and

Onion Architecture- Entity Framework Code First Models DataAnnotations

泪湿孤枕 提交于 2019-12-17 18:43:51
问题 I am developing a ASP.NET MVC Project following the Onion Architecture. I have added the Models inside my Core Project and these Models will be referred as the POCO classes for the Entity Framework Models in the Infrastructure Project. My question is how can I add Data Annotations Which depends on the Entity Framework? Can I make the Core Models as Interfaces and inherit it in the Infrastructure Projects and do real Implementation? 回答1: You don't need to create Core Models as Interfaces if

Adding DataAnnontations to Generated Partial Classes

蹲街弑〆低调 提交于 2019-12-17 18:24:41
问题 I have a Subsonic3 Active Record generated partial User class which I've extended on with some methods in a separate partial class. I would like to know if it is possible to add Data Annotations to the member properties on one partial class where it's declared on the other Subsonic Generated one I tried this. public partial class User { [DataType(DataType.EmailAddress, ErrorMessage = "Please enter an email address")] public string Email { get; set; } ... } That examples gives the "Member is

ASP.NET MVC data annotations client side validation with inherited RegularExpressionAttribute

老子叫甜甜 提交于 2019-12-17 17:58:05
问题 To keep my model validation clean I would like to implement my own validation attributes, like PhoneNumberAttribute and EmailAttribute . Some of these can favorably be be implemented as simple classes that inherit from RegularExpressionAttribute . However, I noticed that doing this breaks client side validation of these attributes. I am assuming that there is some kind of type binding that fails somewhere. Any ideas what I can do to get client side validation working? Code example : public