data-annotations

How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

喜夏-厌秋 提交于 2019-11-27 07:36:30
I'm building a MVC web application with C#. Since the site will be multilingual, I've implemented my own ResourceManager. This class is responsible for fetching the required resource strings from a database/cache depending on the currents thread culture and works fine so far. My problem is, I'd like to use the my custom ResourceManager solution to fetch validation error messages when for example using the Required Attribute on a property. Can this be done? The RequiredAttribute allows to use a custom resource manager : [Required( ErrorMessageResourceType = typeof(CustomResourceManager),

Annotating properties on a model with default values

爷,独闯天下 提交于 2019-11-27 07:16:22
I created an EF4.1 code-first model (may or may not be important), and I'm trying to get default values for my Create scaffold template. My model looks like: class Person { [DefaultValue (18)] public int Age { get; set; } } And then my Create view looks like: <div class="editor-label"> @Html.LabelFor(model => model.Age) </div> <div class="editor-field"> @Html.EditorFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age) </div> I would expect at runtime, that the EditorFor would pre-populate the textbox with "18", but it does no such thing. Am I misunderstanding what the

Custom Model Validator for Integer value in ASP.NET Core Web API

北城余情 提交于 2019-11-27 07:08:02
问题 I have developed a custom validator Attribute class for checking Integer values in my model classes. But the problem is this class is not working. I have debugged my code but the breakpoint is not hit during debugging the code. Here is my code: public class ValidateIntegerValueAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { if (value != null) { int output; var isInteger = int.TryParse(value.ToString(), out

How does DataAnnotations really work in MVC?

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:56:12
This is more of a theoretical question. I'm currently examining the MVC 3 validation by using ComponentModel.DataAnnotations, and everything works automagically, especially on client side. Somehow something checks for those attributes, and generates javascript for the validation (or html5 attributes, if using unobtrusive mode), and it works. My question is that what generates the client side javascript and how can I access and modify it? For example I want to handle the given dataannotation attributes a little differently, or handle custom attributes (I have found that I can derive them from

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

僤鯓⒐⒋嵵緔 提交于 2019-11-27 06:36:53
问题 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;} 回答1: [DisplayFormat(DataFormatString="{0:#.####}")] See Custom Format Strings for formats and DisplayFormatAttribute for examples 来源: https

How to catch DataAnnotations Validation in MVVM

北城以北 提交于 2019-11-27 06:30:21
问题 How do i catch the validation from the DataAnnotations ? i research here but i didn't understand how it works so i hoppe some of you can enlighten my here my current test code: Model public class Person // Represents person data. { /// <summary> /// Gets or sets the person's first name. /// </summary> /// <remarks> /// Empty string or null are not allowed. /// Allow minimum of 2 and up to 40 uppercase and lowercase. /// </remarks> [Required] [RegularExpression(@"^[a-zA-Z''-'\s]{2,40}$")]

How I can find Data Annotation attributes and their parameters using reflection

醉酒当歌 提交于 2019-11-27 06:03:33
问题 I have some data annotation attribute like this: [StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")] How I can Find Data Annotation attributes and their parameters using reflection? thanks 回答1: I assume you have something like this: [StringLength(20, MinimumLength = 5, ErrorMessage = "First name must be between 5 and 20 characters")] public string FirstName {get;set;} To get the attribute and a property from it: StringLengthAttribute

Entity Framework Code First Fluent Api: Adding Indexes to columns

拟墨画扇 提交于 2019-11-27 05:58:21
I'm running EF 4.2 CF and want to create indexes on certain columns in my POCO objects. As an example lets say we have this employee class: public class Employee { public int EmployeeID { get; set; } public string EmployeeCode { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime HireDate { get; set; } } We often do searches for employees by their EmployeeCode and since there are a lot of employees it would be nice to have that indexed for performance reasons. Can we do this with fluent api somehow? or perhaps data annotations? I know it is

Is it possible to use DataAnnotations with Interfaces?

 ̄綄美尐妖づ 提交于 2019-11-27 05:44:28
问题 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? 回答1: I'm surprised no one mentioned

ASP.NET Core MetaDataType Attribute not working

半腔热情 提交于 2019-11-27 05:15:10
I'm using the MetaDataType Attribute on my domain model class. It it supposed to move the attribute information from the referenced class into the class that the MetadataType attribute has been set. But it doesn't do as advertised. What is causing the issue here? [MetadataType(typeof(ComponentModelMetaData))] public partial class Component { public int Id { get; set; } public string Name { get; set; } public ICollection<Repo> Repos { get; set; } public string Description { get; set; } } public class ComponentModelMetaData { [Required(ErrorMessage = "Name is required.")] [StringLength(30,