1

My property name is "UglyPropertyName" and I want to change the Display Name to "New Role" in my view when using

@Html.DisplayNameFor(Function(model) model.UglyPropertyName)

With C#, putting [Display(Name="Some New Name")] above the class property will change the display name. What is the syntax to do this in VB.net?

Public Class SomeVBClass
    Private _UglyPropertyName As String

    Public Property UglyPropertyame As String
        Get
            Return _UglyPropertyName
        End Get
        Set(value As String)
            _UglyPropertyName = value
        End Set
    End Property

End Class

//Snippet From .vbhtml view

<dt>
    @Html.DisplayNameFor(Function(model) model.UglyPropertyName)
</dt>
<dd>
    @Html.DisplayFor(Function(model) model.UglyPropertyName)
</dd>

Currently the @Html.DisplayNameFor will use the property name "UglyPropertyName". I want it to be something like "Formatted Property Name".

Nate
  • 15
  • 5

1 Answers1

1

It's just slightly different syntax

Public Class SomeVBClass
    Private _UglyPropertyName As String

    <DisplayName("Some New Name")>
    Public Property UglyPropertyame As String
        Get
            Return _UglyPropertyName
        End Get
        Set(value As String)
            _UglyPropertyName = value
        End Set
    End Property

End Class
djv
  • 15,168
  • 7
  • 48
  • 72
  • That was my first guess, but that won't even compile. I have also tried `````` Which will compile, but doesn't alter the displayed name. – Nate Jun 27 '19 at 19:27
  • If it works on your C# then check for a missing namespace – djv Jun 27 '19 at 19:29
  • Try the attribute `DisplayName` – djv Jun 27 '19 at 19:33
  • Thank you. The final solution was to import ```System.ComponentModel``` and use the syntax `````` – Nate Jun 27 '19 at 19:39
  • See [this answer](https://stackoverflow.com/a/41030149/832052) which shows where each attribute Display and DisplayName are located. DisplayName is located in `System.ComponentModel.DataAnnotations`. They should both do the same thing. – djv Jun 27 '19 at 21:08
  • 1
    @Nate usually when this happens (at least in VS 2017) if you hover over the red squigglies, there will be a widget that suggests ways to fix the problem, and it will usually be able to either qualify the reference with the right namespace, or add the right `Imports` directive for you (or even tell you which reference you need to add to your project). – Craig Jun 28 '19 at 18:56