Mukesh Bhojwani
Mukesh Bhojwani

Reputation: 2024

MVC 6 vNext Display Name attribute not working

The Display Name attribute not working vNext.

CustomerEntryModel

public class CustomerEntryModel
{
    [DisplayName("First Name")]
    public String FirstName { get; set; }
}

CustomerEntry.cshtml

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
@model CustomerEntryModel

    <table>
        <tr>
            <td>
                <label asp-for="FirstName"></label>
            </td>
            <td>
                <input type="text" asp-for="FirstName"/>
            </td>
        </tr>
    </table>

Rendered HTML

<table>
        <tbody>
        <tr>
            <td><label for="FirstName">FirstName</label></td>
            <td>
                <input type="text" name="FirstName" value="">
            </td>
        </tr>
        </tbody>
</table>

The following post shows how to use tag helper on the label to print the display name. But, this uses DisplayAttribute, which works for me too.

http://www.davepaquette.com/archive/2015/05/18/mvc-6-label-tag-helper.aspx

But, I want to use DisplayNameAttribute. The reason is, I want to extend the behavior of the attribute, and retrieve the display name from database. I can't extend DisplayAttribute because it is sealed.

How to do this?

Upvotes: 3

Views: 1558

Answers (1)

Amir
Amir

Reputation: 2108

I had the same issue. I was using Asp.NET Core Version 1.0.0 (using .net framework 4.6.1 as dependency) and DisplayName was not working (it was rendering the the property name instead of the DisplayName value). I upgraded my MVC packages to 1.1.0-preview1-final and it was solved.

It seems to me there was a bug and they are fixing it in the next version(s). I will keep developing using the previews as I use the DisplayName for extending DisplayAttribute as well.

Upvotes: 1

Related Questions