data-annotations

How to get metadata custom attributes?

牧云@^-^@ 提交于 2019-12-07 05:21:21
问题 I have a class that defines data annotations at class level. The meta data class has custom attributes associated with it, along with the usual DisplayName, DisplayFormat etc. public class BaseMetaData { [DisplayName("Id")] public object Id { get; set; } [DisplayName("Selected")] [ExportItem(Exclude = true)] public object Selected { get; set; } } [MetadataType(typeof(BaseMetaData))] public class BaseViewModel { public int Id { get; set; } public bool Selected { get; set; } Given a type T ,

How to make entry field to allow numbers only using EF and Data Annotations?

与世无争的帅哥 提交于 2019-12-07 03:54:02
问题 I am trying to figure out if there is a way to make sure that numeric only input is allowed using Data Annotations and Entity Framework. I am using the following code [Required] [DisplayName("Client No")] [Column("client_no", TypeName = "smallint")] public virtual Int16 Number { get; set; } I want this to be displayed using number class. In one place I use the following <input type="number" name="searchClientNo" class="numericOnly" /><br /> but in the entry form I am using @Html.EditorFor(m =

changing viewmodel's MetadataType attribute at runtime

只谈情不闲聊 提交于 2019-12-07 03:50:58
问题 In Microsoft MVC 3.0,I have a class: public class Product{ public string Title {get;set;} } This class can be represent as Product or as Service , the only difference between them is just the field labels at View time. so I create two classes : public class ProductMetaData { [Display(Name = "Product")] public object Title { get; set; } } and public class ServiceMetaData { [Display(Name = "Service")] public object Title { get; set; } } How can I set these classes at runtime as MetadataType ? -

what are the differences between [DataType(DataType.EmailAddress)] & [EmailAddress]

≡放荡痞女 提交于 2019-12-07 03:35:45
问题 I am trying to understand what are the main differecnes between using [DataType(DataType.EmailAddress)] & [EmailAddress] . inside a model class:- public class MYViewModel { [DataType(DataType.EmailAddress)] OR [EmailAddress] public string Email { get; set; } i did a test and the two attributes will do the following:- will prevent users from adding invalud email address will display the value as "EmailTo:..." but i can not find any differences in respect to the functionality , of course if i

Support for nested model and class validation with ASP.NET MVC 2.0

巧了我就是萌 提交于 2019-12-07 02:15:00
问题 I'm trying to validate a model containing other objects with validation rules using the System.ComponentModel.DataAnnotations attributes was hoping the default MVC implementation would suffice: var obj = js.Deserialize(json, objectInfo.ObjectType); if(!TryValidateModel(obj)) { // Handle failed model validation. } The object is composed of primitive types but also contains other classes which also use DataAnnotications. Like so: public class Entry { [Required] public Person Subscriber { get;

Data Annotations - using an attribute extension and storing regular expressions in a resource file

荒凉一梦 提交于 2019-12-07 02:14:45
问题 I am currently working with MVC4 data annotations to handle validation. I am working on a site that will be very much international and as such I keep all of my text in resource files. I also want to keep regular expressions for validation in resource files so I can use the same code to check, for example, Post Codes (UK) and Zip Codes (US) just by using a different RegEx (and resources for the different names etc). I have the below attribute which is already pulling the error message from a

why does ASP.Net MVC Range Attribute take a Type?

自作多情 提交于 2019-12-07 01:40:29
问题 I was just wondering why the Range validation attribute can take a Type and two strings as parameters? Is this for validating strings against an Enum or something like this? Also what I am trying to do is find an easy way to validate a 3 character string which must be present in an enum, any sugestions? Thanks, Alex. 回答1: I did find the Range ctor you mentioned fishy. Couldn't help but investigate. (So I wrote this answer like a log while investigating.) From MSDN public RangeAttribute( Type

Is it possible to force the currency for an MVC3 field with DataType as DataType.Currency

感情迁移 提交于 2019-12-06 21:36:44
问题 I'm writing an MVC3 application that reads in a bunch of monetary data from a database. The issue I have is that these amounts are all in different currencies. If I set the type of a field like this: [DataType(DataType.Currency)] public Amount{ get; set;} I get the decimal places and a currency symbol, which looks nice, but it defaults to the user's local currency. A US user sees $423.29 whereas a GB user sees £423.29 . I can override the currency by using a <globalization culture="{something

System.ComponentModel.DataAnnotations.Schema not found

99封情书 提交于 2019-12-06 20:55:29
问题 I am running into an issue in Visual Studio 2012 that involves the System.ComponentModel.DataAnnotations.Schema namespace. It tells me that the ForeignKeyAttribute cannot be resolved, the solution in the past was to add the using statement that is commented out below. VS2012 can't resolve the Schema namespace as VS2010 was able to. Has anything changed in recent .Net releases that would be causing this problem? If so, how do I work around them? using System; using System.Collections.Generic;

ASP.net core MVC 6 Data Annotations separation of concerns

这一生的挚爱 提交于 2019-12-06 19:54:30
I want put the Data Annotations Attribute and the IClientValidatable interface in two seperate assemblies to have separation of concerns. One is called Common and the other Comman.Web. These links explain how it works in MVC 5: Keeping IClientValidatable outside the model layer http://www.eidias.com/blog/2012/5/25/mvc-custom-validator-with-client-side-validation Unfortunately in MVC 6 there is no DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(MyValidationAttribute), typeof(MyValidationAttributeAdapter) ); How does it work in ASP.net core MVC 6? I use the RC1. In Startup.cs , in