data-annotations

What is the proper data annotation to format my decimal property?

我怕爱的太早我们不能终老 提交于 2019-11-28 11:56:36
I have a POCO with a decimal property called SizeUS. I would like to use data annotations to format the display of the decimal in a view. My SizeUS property is only displaying 2 decimal places in my view and I want it to display 4 decimal places. What is the proper data annotation to accomplish this ? [DisplayFormat( ? )] public decimal SizeUS {get; set;} [DisplayFormat(DataFormatString="{0:#.####}")] See Custom Format Strings for formats and DisplayFormatAttribute for examples 来源: https://stackoverflow.com/questions/20848969/what-is-the-proper-data-annotation-to-format-my-decimal-property

Where are the Entity Framework t4 templates for Data Annotations?

让人想犯罪 __ 提交于 2019-11-28 10:48:38
I have been googling this non stop for 2 days now and can't find a single complete, ready to use, fully implemented t4 template that generates DataAnnotations. Do they even exist? I generate POCOs with the standard t4 templates. The actual database table has metadata that describes some of the validation rules, eg not null, nvarchar(25), etc. So all I want is a t4 template that can take my table and generate a POCO with DataAnnotations, eg public class Person { [Required] [StringLength(255)] public FirstName {get;set} } It is a basic and fundamental requirement, surely I can not be the first

How to use ASP.Net core ModelMetadata attribute

*爱你&永不变心* 提交于 2019-11-28 09:30:19
问题 [Table("LegalEntity")] [ModelMetadataType(typeof(LegalEntityMeta))] public class LegalEntity : Entity<long> { } public class LegalEntityMeta { [JsonProperty(PropertyName = "LegalEntityId")] public long Id { get; set; } [JsonProperty(PropertyName = "LegalEntityName")] public string Name { get; set; } } In the Startup.cs .... services .AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); })

Class-level validation

女生的网名这么多〃 提交于 2019-11-28 08:57:36
问题 I am validating a class with DataAnnotations utils. I have a class that has a Title property and an Item property. I want to apply a RequiredAttribute to the Title property but it should be invalid only if the Item property is null; if the Item property is set with an object, the Title is not required. In short words, I want the RequiredAttribute to validate only if a condition in the class is satisfied. How can this be done. Update As I didn't find other way, and since I usually don't need

Why are buddy classes used for validation?

余生长醉 提交于 2019-11-28 08:38:48
问题 I am curious as to why data validation is done using buddy classes. Consider the following example, where MyEntity is a Linq-to-SQL or Linq-to-Entities entity, and the class below is a partial class enhancing the entity. [MetadataType(typeof(MyEntity.MyEntityMetadata))] public partial class MyEntity { private class MyEntityMetadata { [Required(ErrorMessage = "The title is required.")] public string Title { get; set; } } } Why is the design so? When they designed DataAnnotations, why was this

Many to Many Mapping Using Data Annotations

给你一囗甜甜゛ 提交于 2019-11-28 08:03:19
问题 [Table("UserMaster")] public class UserMaster { public UserMaster() { this.Roles = new List<Role>(); } [Key] public int UserId { get; set; } public string UserName { get; set; } public ICollection<Role> Roles { get; set; } } [Table("Role")] public class Role { public Role() { this.Users = new List<UserMaster>(); } public int RoleId{ get; set; } public string Name{ get; set; } public ICollection<UserMaster> Users { get; set; } } I map these tables using EntityTypeConfiguration and it triggers

localize default model validation in mvc 2

空扰寡人 提交于 2019-11-28 07:41:09
[Required] [DisplayName("my date")] public DateTime? DateReg { get; set; } so if the user is going to pass in an invalid datetime value he will get this message "The value '02.07.201022' is not valid for my date." how can I translate/localize this message ? Add Messages.resx in App_GlobalResources and in Application_Start in Global.asax : DefaultModelBinder.ResourceClassKey = "Messages"; Then in the Messages.resx file you could define the following string: PropertyValueInvalid: The value {0} is invalid for the property {1} The key needs to be called PropertyValueInvalid . 来源: https:/

Can't get MVC 4 FileExtensions Attribute to work on ViewModel property

有些话、适合烂在心里 提交于 2019-11-28 07:27:43
问题 I need to upload a csv file and I want to restrict it's extension to .csv So I added the follow property to my ViewModel: [FileExtensions(ErrorMessage = "Must choose .csv file.",Extensions = "csv,txt")] public HttpPostedFileBase File { get; set; } In my view I have the following: @Html.TextBoxFor(m => m.File, new { type = "file"}) @Html.ValidationMessageFor(m => m.File) However as soon as it hits my "ModelState.IsValid" check it returns invalid with my error message of "Must choose .csv file.

Is it possible to use DataAnnotations with Interfaces?

微笑、不失礼 提交于 2019-11-28 07:22:47
I want to use DataAnnotations to validate classes that implements some interfaces, and so I'm adding validation attributes to the interface, like this: public interface IUser { [Required] string Name { get; set; } [Display(Name = "Email Address")] [Required] string Email { get; set; } } It doesn't work when I try to use Validator.TryValidateObject . Is there any way to make this instead of having to write a custom TryValidateObject method? I'm surprised no one mentioned MetadataTypeAttribute . But yes, this works. [MetadataType(typeof(ICustomerMetaData))] public partial class Customer { }

Adding DataAnnontations to Generated Partial Classes

倖福魔咒の 提交于 2019-11-28 07:04:21
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 already defined" error. I think I might have seen an example a while ago of what I'm trying to do with