Metadata were not loaded using MetadataType
To give some part of the answer , you can check ClientInfoView has got attributes. Some small demo that worked for me. Still trying to find why I can't access those attributes in ClientInfoViewMetaData individual properties
static void Main(string[] args)
{
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ClientInfoView), typeof(ClientInfoViewMetaData)), typeof(ClientInfoView));
ClientInfoView cv1 = new ClientInfoView() { ID = 1 };
var df = cv1.GetType().GetCustomAttributes(true);
var dfd = cv1.ID.GetType().GetCustomAttributes(typeof(DisplayNameAttribute), true);
var context = new ValidationContext(cv1, null, null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject( cv1,context, results, true);
}
}
[MetadataType(typeof(ClientInfoViewMetaData))]
public partial class ClientInfoView
{
public int ID { get; set; }
public string Login { get; set; }
}
public class ClientInfoViewMetaData
{
[Required]
[Category("Main Data"), DisplayName("Client ID")]
public int ID { get; set; }
[Required]
[Category("Main Data"), DisplayName("Login")]
public string Login { get; set; }
}
Metadata were not loaded using MetadataType
To give some part of the answer , you can check ClientInfoView has got attributes. Some small demo that worked for me. Still trying to find why I can't access those attributes in ClientInfoViewMetaData individual properties
static void Main(string[] args)
{
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(typeof(ClientInfoView), typeof(ClientInfoViewMetaData)), typeof(ClientInfoView));
ClientInfoView cv1 = new ClientInfoView() { ID = 1 };
var df = cv1.GetType().GetCustomAttributes(true);
var dfd = cv1.ID.GetType().GetCustomAttributes(typeof(DisplayNameAttribute), true);
var context = new ValidationContext(cv1, null, null);
var results = new List<ValidationResult>();
var isValid = Validator.TryValidateObject( cv1,context, results, true);
}
}
[MetadataType(typeof(ClientInfoViewMetaData))]
public partial class ClientInfoView
{
public int ID { get; set; }
public string Login { get; set; }
}
public class ClientInfoViewMetaData
{
[Required]
[Category("Main Data"), DisplayName("Client ID")]
public int ID { get; set; }
[Required]
[Category("Main Data"), DisplayName("Login")]
public string Login { get; set; }
}
DataAnnotations MetadataType not working
Attribute.IsDefined(currentProp, typeof(DisplayAttribute))is still False
IT MUST BE FALSE. Probably you are checking the Order with a custom code! The answer to your problem is very easy: your custom code is simply WRONG. Probably it is wrong because you pretend to find the attribute added with the methadata class together with all other "native" attributes of the class. THIS IS WRONG! .Net Clr HAS NO NATIVE SUPPORT FOR MetaDataType Attribute! IT IS JUST A CONVENTION. It is up to you, verifyng that your class has a MetaDataType and retrieving also the attributes of the MetaDataType with the same name of the properties of your original class.
I MEAN YOU HAVE TO DO THIS JOB MANUALLY. All Attributes that the Mvc engine handles automatically are handled this way...that is the Mvc Engine look at the attributes of the MetaDataType and merge them with the native attributes...You have to do the same in your custom code.
That said if you need your attribute in a view...instead of retrieving manually your attributes, write a custom MetaDataProvider. The metadata provider logics automatically retrieve for you all attributes(the way I explained)...you need just to specify what action to take for each of them.
ASP.NET Core MetaDataType Attribute not working
ASP.NET Core uses
Microsoft.AspNetCore.Mvc **ModelMetadataType**
instead of
System.ComponentModel.DataAnnotations.**MetadataType**
source
Try changing your attribute to [ModelMetadataType(typeof(ComponentModelMetaData))]
MetadataType and DataAnnotations not Working
Partial classes can only exist within one project.
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
More specifically,
All partial-type definitions meant to be parts of the same type must
be defined in the same assembly and the same module (.exe or .dll
file). Partial definitions cannot span multiple modules.
Related Topics
Getting a System.Type from Type's Partial Name
Find Out Username(Who) Modified File in C#
How to Create a Custom Attribute in C#
Check If Property Has Attribute
Mvcbuildviews Not Working Correctly
Nice & Universal Way to Convert List of Items to Tree
Fluent API, Many-To-Many in Entity Framework Core
Running Msbuild Programmatically
How to Deserialize a Unix Timestamp (Μs) to a Datetime from JSON
Formatting a Double to Two Decimal Places
Force JSON.Net to Include Milliseconds When Serializing Datetime (Even If Ms Component Is Zero)
Accessing Properties Through Generic Type Parameter
Jcontainer, Jobject, Jtoken and Linq Confusion
ASP.NET Webapi2 Enable Cors Not Working with Aspnet.Webapi.Cors 5.2.3