Search code examples
c#asp.net-mvcdata-annotationspartial-classesmetadatatype

MetadataType don't work with partial class in ASP.NET MVC


Hi I've a problem with MetadataType with my partial class

In my asp.net mvc project i have a class library (Infrastructure) with a DB directory who contain my partial class scafolded from the DB

Exemple

namespace BibliEasy.Infrastructure.DB
{
    public partial class Series
    {
        public Series()
        {
            Publications = new HashSet<Publication>();
        }

        public int IdSerie { get; set; }

        public string TitreSerie { get; set; }

        public string StatutSerie { get; set; }

        public int? VolumesSerie { get; set; }

        public virtual ICollection<Publication> Publications { get; set; }
    }
}

I add Metadata file for the data annotation

Exemple

namespace BibliEasy.Infrastructure.DB
{
    [MetadataType(typeof(SeriesMetaData))]
    public partial class Series { }

    public class SeriesMetaData
    {
        [Display(Name = "Titre")]
        [Required]
        [StringLength(255)]
        public string TitreSerie { get; set; }

        [Display(Name = "Statut")]
        [Required]
        [StringLength(50)]
        public string StatutSerie { get; set; }

        [Display(Name = "Nombre total de volume")]
        [Range(0, int.MaxValue)]
        public int? VolumesSerie { get; set; }
    }
}

And it don't work, in the view this is the name of Serie's property and not the display from SeriesMetaData and the Validation don't work.

What am I missing?

for information the architecture of my project

Infrastructure class library

  • DB directory

Domaine class library

  • Services directory with the class who contains acces function to DB

Application class library

  • ViewModels directory
  • Services directory who contains controller of view models

MVCApp

  • Areas
  • Controllers
  • Views

Solution

  • Ok in fact I'm in ASP.NET Core so it's not MetadataType but ModelMetadataTypeAttribute