0

I am fairly new to Razor Pages and even .Net Core so please bear with me.

I am using Dapper to fill a class with data from an SQL Database which is working great. I can't change the long existing field names. So when I try to get headlines in a table by cycling through the properties and displaying the names in the headlines, I end up with the fairly cryptic field names. I try to add Data Annotiations to the class properties, but those do not seem to catch on to the names of the properties.

My class with annotiations:

namespace HelloRazorPage.Models
{
    [BindProperties]
    public class Apotheke
    {
        [Display(Name ="KdNr")]
        public string KUNDENNUMM { get; set; }

        public string BGANr { get; set; }

        [Display(Name = "Name")]
        public string NAMA { get; set; }

        [Display(Name = "Straße")]
        public string STRASSE{ get; set; }

        [Display(Name = "Haus Nr.")]
        public string HAUSNR { get; set; }

        public string PLZ { get; set; }

        [Display(Name = "Ort")]
        public string ORT { get; set; }

    
        public List<Vertraege> Vertraege { get; set; }

        public bool IsNull { get; set; }

    }
}

And cycling through my properties:

<table class="tableScroll">                
         @{var apo = (Models.Apotheke)ViewData["Apo"];  }
         @if (apo != null)
         {
           
                 <thead>
                     <tr>
                           @foreach(var prop in apo.GetType().GetProperties())
                            {<th> @prop.Name </th>}
                     </tr>

                 </thead>

This works like it's intendet to. It just does not show the annotiatons.

Tried to add data annotiatons to my class and want them to show up in the name property of [object].apo.GetType().GetProperties()

  • 1
    Does this answer your question? [How to retrieve Data Annotations from code? (programmatically)](https://stackoverflow.com/questions/7027613/how-to-retrieve-data-annotations-from-code-programmatically) – Marco Dec 02 '22 at 14:17

0 Answers0