Viktorija Viktorija - 3 years ago 210
C# Question

Asp.Net MVC MetaDataType Attribute not working

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, MinimumLength = 3, ErrorMessage = "Name length should be more than 3 symbols.")]
public string Name { get; set; }
public ICollection<Repo> Repos { get; set; }
[Required(ErrorMessage = "Description is required.")]
public string Description { get; set; }
}

Answer Source

ASP.NET Core uses the
Microsoft.AspNetCore.Mvc.Core.ModelMetadataType instead of System.ComponentModel.DataAnnotations.MetadataType:
source

Try changing your attribute to [ModelMetadataType(typeof(ComponentModelMetaData))]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download