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?


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







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

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy