data-annotations

Entity Framework 4.1 InverseProperty Attribute

ε祈祈猫儿з 提交于 2019-11-26 09:25:17
问题 Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. Does anyone know any useful resources about the scenarios that this attribute becomes useful? Should I use this attribute on navigation properties? example: public class Book { public int ID {get; set;} public string Title {get; set;} [ForeignKey(\"FK_AuthorID\")] public Author Author {get; set;} } public class Author { public int ID {get; set;}

DataAnnotations dynamically attaching attributes

无人久伴 提交于 2019-11-26 09:17:29
问题 Apparently it is possible to dynamically attach DataAnnotation attributes to object properties at runtime and as such achieve dynamic validation. Can someone provide code sample on this? 回答1: 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

Assign format of DateTime with data annotations?

左心房为你撑大大i 提交于 2019-11-26 09:07:24
问题 I have this attribute in my view model: [DataType(DataType.DateTime)] public DateTime? StartDate { get; set; } If I want to display the date, or populate a textbox with the date, I have these: <%: Model.StartDate %> <%: Html.TextBoxFor(m => m.StartDate) %> Whenever the date is displayed, it\'s displayed like: 01/01/2011 12:00:00 AM But I\'d like to only display 01/01/2011 Is there a way to apply a display format with data annotations? I don\'t want to have to go to every instance where I

Entity Framework code first unique column

北城以北 提交于 2019-11-26 08:59:00
问题 I am using Entity Framework 4.3 and using Code Fist. I have a class public class User { public int UserId{get;set;} public string UserName{get;set;} } How do I tell Entity Framework that UserName has to be unique when creating database table? I would prefer to use data anotations instead of configuration file if possible. 回答1: In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations

MVC Model require true

梦想与她 提交于 2019-11-26 08:49:58
问题 Is there a way through data annotations to require that a boolean property be set to true? public class MyAwesomeObj{ public bool ThisMustBeTrue{get;set;} } 回答1: You could create your own validator: public class IsTrueAttribute : ValidationAttribute { #region Overrides of ValidationAttribute /// <summary> /// Determines whether the specified value of the object is valid. /// </summary> /// <returns> /// true if the specified value is valid; otherwise, false. /// </returns> /// <param name=

MVC Validation Lower/Higher than other value

半城伤御伤魂 提交于 2019-11-26 08:10:44
问题 How is the best way to validate a model in MVC.Net where I want to accept a minimum/maximum. Not individual min/max values for a field. But separate fields for a user to specify a minimum/maximum. public class FinanceModel{ public int MinimumCost {get;set;} public int MaximumCost {get;set;} } So I need to ensure that MinimumCost is always less than Maximum cost. 回答1: You can use a custom validation attribute here is my example with dates. But you can use it with ints too. First, here is the

Custom model validation of dependent properties using Data Annotations

余生颓废 提交于 2019-11-26 06:34:07
问题 Since now I\'ve used the excellent FluentValidation library to validate my model classes. In web applications I use it in conjunction with the jquery.validate plugin to perform client side validation as well. One drawback is that much of the validation logic is repeated on the client side and is no longer centralized at a single place. For this reason I\'m looking for an alternative. There are many examples out there showing the usage of data annotations to perform model validation. It looks

How to create Custom Data Annotation Validators

倾然丶 夕夏残阳落幕 提交于 2019-11-26 05:28:31
问题 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

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

不问归期 提交于 2019-11-26 04:27:58
问题 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; } 回答1: 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

ASP.NET MVC: Custom Validation by DataAnnotation

谁说胖子不能爱 提交于 2019-11-26 03:17:18
问题 I have a Model with 4 properties which are of type string. I know you can validate the length of a single property by using the StringLength annotation. However I want to validate the length of the 4 properties combined. What is the MVC way to do this with data annotation? I\'m asking this because I\'m new to MVC and want to do it the correct way before making my own solution. 回答1: You could write a custom validation attribute: public class CombinedMinLengthAttribute: ValidationAttribute {