Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How change Displayname value at runtime based on other parameter value in MVC?

Tags:

asp.net-mvc

[DisplayName("Planned Amt($):")]  
 public string PlannedAmount { get; set; } 
 [DisplayName("Unallocated Amt($):")] 
 public string UnallocatedAmount { get; set; }

I have this members in my class .Based on budgetType variable value i need to change the DisplayName Value for both the attributes. Please, let me know how to do that.

like image 886
hari Avatar asked May 31 '11 15:05

hari


1 Answers

You should just be able to inherit from DisplayNameAttribute and override the DisplayName property:

public class DisplayNameExAttribute : DisplayNameAttribute
{
    public override string DisplayName
    {
        get
        {
            // do what you want here.
            return base.DisplayName;
        }
    }
}

I haven't actually tried to implement this, so it is possible that the DisplayName attribute is coded such that some other extension point will have to be used, but I'll try to confirm.

like image 197
Hal Avatar answered Sep 29 '22 09:09

Hal