asp.net-mvcdisplayattribute

Multiple MVC3 Display attributes


I have a property in a model. Lets say for an Email.

    <Display(Name:="Email")>
    <DataMember()>
    Public Property Email As String

I want to use a different label for the email in every page so basically I'm asking if there is a way to use two display attributes for the same field? I would like to have something like this -

    <Display(Name:="Email")>
    <Display(Name:="Please Enter your Email")>
    <DataMember()>
    Public Property Email As String

and then choose in the page what label to use.

Is there a way, I can achieve this?


Solution

  • [DisplayName("Please Enter your Email")] //cannot specify a ResourceType
    
    [Display(Name:="Please Enter your Email")]
    

    Attributes are read at compile time and become part of the assembly metadata. It seems like you want to decide at runtime what label to use.

    For that, in your view, you could do something like..

    @if(crazyUser){
        <label for="Email">Alternate Label</label>
    }
    else{    
        @Html.LabelFor(m => m.Email)    
    }
    

    So that's the extra bit apart from the [Display] attribute.