I've seen many similar questions, but none seem to have a direct answer. When I attempt to add a Display(Name) attribute to a foreign key field, the Display Name isn't shown on the Create, Edit, Delete and Details form. I tried putting the attribute on the navigation property as well:
[Display(Name="Gender")]
public virtual Gender Gender {get; set;}
but that didn't work either.
public class Person
{
public int ID {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
[Display(Name="Gender")]
public int GenderID {get; set;}
public virtual Gender Gender {get; set;}
}
public class Gender
{
public int ID {get; set;}
public string GenderName {get; set;}
public virtual ICollection<Person> People {get; set;}
}
The solution is simple. After you add the display attribute in the model, remove the label name from the view.
So Change
@Html.LabelFor(model => model.GenderID, "GenderID", htmlAttributes: new { @class = "control-label col-md-2" })
to
@Html.LabelFor(model => model.GenderID, htmlAttributes: new { @class = "control-label col-md-2" })
in your view. This problem occurs when you scaffold your view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With