Reputation: 719
My questions is: I have an enum class as:
public enum EnumApplications
{
Android = 1,
IOS = 2
}
Also I have a Model which is for Creating a new user. a user can be defined as Active in any of these application when we create a new user.
My model is:
public class NewUserModel
{
public int UserId { get; set; }
[Required]
[Display(Name = "FirstName")]
public string FirstName { get; set; }
[Required]
[Display(Name = "LastName")]
public string LastName { get; set; }
public int ClientId { get; set; }
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string EmailAddress { get; set; }
[Display(Name = "Is Active in IOS")]
public bool IsActiveInApp1 { get; set; }
[Display(Name = "Is Active in Android")]
public bool IsActiveInApp2 { get; set; }
}
What I want to do is in the: [Display(Name = "Is Active in Android")]
I want to use the enum so if the app name gets changed I don't need to change it here, something like:
EnumApplications.IOS.ToString()
Here's a screenshot for better understanding:
Upvotes: 1
Views: 1703
Reputation: 1504
"I want to use the enum so if the app name gets changed I don't need to change it here"
No, there is no easy way to do this.
the only implementation I can think of (aside from runtime assembly/type compiling and generation) that you could do that, is a T4 template. the template would generate the model, and you would have to run it after modifying your enum
.
in regards to your question title and comment:
MVC - Using Enum In The DisplayAttribute of a Model class
what I want is in my model instead of saying [Display(Name = "Is Active in IOS")] and hardcoding IOS. I want to get the name of IOS from my enum.
The following solution (which isn't T4 or Runtime compiling) just inherits DisplayNameAttribute
and uses the constructor of your type to pass the parameter to the base type.
It is a solution in the sense that if you were to change Android
to Andy
you would use a refactor tool ( built-in intellisense tool these days) to implement that change across code files that references that value type.
public enum MyEnum
{
NotSet = 0,
iOS = 1,
Android = 2
}
class MyDisplayNameAttribute : System.ComponentModel.DisplayNameAttribute
{
public MyDisplayNameAttribute(MyEnum myEnum)
: base("Is Active in " + myEnum.ToString())
{
}
public override string DisplayName
{
get
{
// you could do the "Is Active in " here, but I doubt control frameworks would use it.
return base.DisplayName;
}
}
}
public class MyModel
{
[MyDisplayName(MyEnum.iOS)]
bool IsActiveApp1 { get; set; }
// uses standard 'DisplayNameAttribute' type for test below
[System.ComponentModel.DisplayName("Is Active in Android")]
bool IsActiveApp2 { get; set; }
}
For quick testing, we'll do what control frameworks do, reflect over the provided type and get the CustomAttribute.. note that there is more type checking involved, we'll casting it to DisplayNameAttribute
for brevity.
string[] PropertyNames = new string[] { "IsActiveApp1", "IsActiveApp2" };
System.Type MyModelInfo = typeof(MyModel);
PropertyNames.SelectMany(prop => MyModelInfo.GetProperty(prop).GetCustomAttributes(true))
.ToList().ForEach((attr) =>
{
Console.WriteLine(((System.ComponentModel.DisplayNameAttribute)attr).DisplayName);
});
Upvotes: 1