Home > Articles > Details

How to Use DisplayFor to Display Text from EF Core Data Annotation in MVC and Components

Question: #DisplayFor is not working in #MVC Asp.Net Core View and the #Component, what am I doing wrong?

I have my #Modal #Class defined as below:

 public sealed partial class CompositionQuestionaire
    {

        [Key]
        public int Id { get; set; }
       
        [Display(Name = "Have there been any changes in household composition in the last twelve months?")]
        public bool AnyChangesInLast12Months { get; set; }
}


And I am trying to render the Display Name as shown below:

<div class="col-6">
 @Html.DisplayFor(a => a.CompositionQuestionaire.AnyChangesInLast12Months)                              
</div>

The above-shown #DisplayFor @HTML Tag Helper in MVC is not working, what am I doing wrong?


Please Login to see the rest of the answer
Answer: Use the @Html.DisplayNameFor instead of DisplayFor, I hope this helps you.

Helpful Resources:
1. https://buildstarted.com/2010/09/29/htmlhelper-guide-for-mvc-3-part-2/

2. https://jakeydocs.readthedocs.io/en/latest/data/index.html




If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Lidia said:

How to contact you?

Posted On: September 17, 2022 13:18:04 PM
Mark said:

Yes

Posted On: September 17, 2022 13:17:36 PM

© 2022 - ErnesTech - Privacy