data-annotations

How can I use a custom ValidationAttribute to ensure two properties match?

邮差的信 提交于 2019-12-11 00:14:04
问题 We're using xVal and the standard DataAnnotationsValidationRunner described here to collect validation errors from our domain objects and view models in ASP.NET MVC. I'd like to have a way to have that validation runner identify when two properties don't match through the use of custom DataAnnotations. Right now I'm forced into doing it outside of the runner, this way: if (!(model.FieldOne == model.FieldTwo)) errors.Add(new ErrorInfo("FieldTwo", "FieldOne must match FieldTwo", model.FieldTwo)

How to create a Must Not Match attribute for email address

a 夏天 提交于 2019-12-10 23:19:32
问题 i am using data annotation to validate my properties, below are the properties for my page public string YourEmail{get;set;} public string AnotherEmail{get;set;} my requirement is that both email id should not be same please suggest what should i use from data annotation to solve this issue thanks, 回答1: You have to implement IValidatableObject interface in your model and add method Validate in your model. public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {

Entlib5 Validation [IgnoreNull] throws exception while adding objects to list

旧街凉风 提交于 2019-12-10 22:43:32
问题 I have a class Partner and I use entlib5 validation block to validate class's fields (using data anottations). In one of method I read db table and create binding list of Partner's objects, but if there is an [IgnoreNull] annotation or [ValidationComposition]+[NotNullValidator] i get an System.NotSupportedException (for example: A validation attribute of type ValidatorCompositionAttribute cannot be used to validate values.) If I use only [StringLengthValidator] there isn't any exception (but

Validate complex types with DataAnnotations

◇◆丶佛笑我妖孽 提交于 2019-12-10 22:20:07
问题 I've decided to use Entity Framework for O/R Mapping, and DataAnnotations for validation in my project, and I have now encountered an odd problem when trying to implement this. This is what I've done: I have the following entity type Contact ******* Int32 Id (not null, Entity Key) Name Name (not null) Address Address (not null) String Phone String Email where Name and Address are complex types defined as follows: Name Address **** ******* String First (not null) String Street (not null)

DataAnnotations tryvalidateobject always returns true

廉价感情. 提交于 2019-12-10 21:41:20
问题 namespace ClassValidation { public class Student { [Required(ErrorMessage = "Name is required")] public String Firstname; [Required(ErrorMessage = "Email is required")] public String personalEmail; } } private static void Main(string[] args) { Student student = new Student(); student.personalEmail = "del"; ValidationContext context = new ValidationContext(student, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(student,

In EF Code First is there a data annotation for description field?

此生再无相见时 提交于 2019-12-10 18:56:19
问题 While designing a table in MS-SQL you have a choice to add a description for each column you add to the table. Is it possible in EF Code First to do same with help of Data Annotations ? 回答1: Before you are using my soultion please read a little bit about: Extended Properties: https://technet.microsoft.com/en-us/library/ms190243(v=sql.105).aspx Attributes: http://www.dotnetperls.com/attribute My solution will allow you to do a shadow description for any property like that: [Description("My

How to format DateTimeOffset in Html.TextBoxFor?

筅森魡賤 提交于 2019-12-10 17:07:23
问题 I have a view built on Bootstrap 3 and ASP.NET MVC 5 for editing user profile information, but it displays in the incorrect format on the form. The database is Neo4j and I'm using Neo4jClient to interact with the database from .NET. Neo4jClient requires the use of DateTimeOffset objects instead of DateTime objects to use Json.NET's serializer for passing to Neo4j's REST interface. Therefore, my model uses a DateTimeOffset object to store the birthday for a user. Here is my form field in my

ModelState.IsValid always true, regardless of DataAnnotations attributes

↘锁芯ラ 提交于 2019-12-10 15:38:48
问题 I'm using the new MVC6 framework with Visual Studio 2015, and suddenly all my Data Annotations stopped working. All of them, without me changing the code. public sealed class RegisterUser { [Required(ErrorMessage = "required")] [RegularExpression(@"^((.|\n)*)$", ErrorMessage = "regex")] [StringLength(32, MinimumLength = 3, ErrorMessage = "length")] public string Name { get; set; } ... } And [Route(Address + "/membership")] public class MembershipController : Controller { // POST [address]

add data annotations programmatically

点点圈 提交于 2019-12-10 15:11:21
问题 I am working on an asp mvc3 app that is using .NET 4 . However the model I want to use is in a class library that is using .NET 3.5 and cannot change. Can I add the .NET 4 data annotations to some properties in my model in the controller programatically to get some easy validation? 回答1: Create your own ModelMetadataProvider and this should do want you want http://www.dotnetcurry.com/ShowArticle.aspx?ID=715 回答2: Interesting, I never try that before but could you declare a partial class in your

EF4 code-first vs model-first with regards to model validation

给你一囗甜甜゛ 提交于 2019-12-10 13:38:01
问题 I'm working on a one-man ASP.NET MVC 3 project (I have complete control over database schema and code), and I'm trying to decide between going database-first and POCO w/ my EF4 models, or if I should go w/ code-first. The main thing I'm trying to achieve is decorating my models with DataAnnotation attributes so that I can enforce schema validation prior to doing any persistence. Looking at Scott Guthrie's article about model validation w/ MVC 2, he talks about article about doing it with code