EF Core DataAnnotations not used in Razor page

26 views Asked by At

I am using Html.LabelFor in my razor page.

The auto generated EF entities I have are:

public partial class Projectinformation
{
    public int Id { get; set; }

   // [Display(Name = "Project Number")] <--- this works but
   // gets overwritten if database changes
    public string Code { get; set; }
}

If I add the DisplayAtribute here it works. However, I want to use an extension outside of the autogenerated code and found this pattern in the MSDN documentation:

public class ProjectMetaData
{
    [Display(Name = "Project Number")] //<--- This does not work
    public string Code { get; set; }
}

[MetadataType(typeof(ProjectMetaData))]
public partial class Projectinformation
{
}

But this does not work (I get Code as the label rather than Project Number). Everything is in the Project.Models namespace. The linked documentation has the metadata class inside the partial class, which I also tried but same result.

Here is the razor code:

@Html.LabelFor(model => model.Code, new { @class = "control-label col-md-2" })

And here is the resulting html:

<label class="control-label col-md-2" for="Code">Code</label>

What am I missing?

0

There are 0 answers