Reputation: 353
I have two tables with this structure, this is the entity framework data model:
public partial class Sensores
{
public int idSensor { get; set; }
public int idTipo { get; set; }
public int idFigura { get; set; }
public string nombre { get; set; }
public virtual TipoSensores TipoSensores { get; set; }
}
public partial class TipoSensores
{
public TipoSensores()
{
this.Sensores = new HashSet<Sensores>();
}
public int idTipo { get; set; }
public string nombre { get; set; }
public virtual ICollection<Sensores> Sensores { get; set; }
}
so I have my metadata class that change the display name
[MetadataType(typeof(TipoSensoresMD))]
public partial class TipoSensores
{
}
public class TipoSensoresMD
{
[Display(Name = "Nombre")]
public string nombre { get; set; }
}
and when I call a
@Html.DisplayNameFor(model => model.nombre)
in a Models.TipoSensores View it shows me "Nombre". that's ok.
but when I call
@Html.DisplayNameFor(model => model.TipoSensores.nombre)
in a Models.Sensores view it also display me the label text "Nombre" I want to change the display name when I use
@Html.DisplayNameFor(model => model.TipoSensores.nombre)
to "Nombre del Sensor" but when I use
@Html.DisplayNameFor(model => model.nombre)
in a Models.TipoSensores View it has to show "Nombre" so... I need two different display names depending from where the property is being called.
Upvotes: 0
Views: 963
Reputation: 11
how I can change the default name generated column
@Html.DisplayNameFor(model => model.NOMBRE_RED)
Upvotes: 1
Reputation: 239240
That's not possible. Display names are all or nothing. MVC doesn't care how the property is being referenced. However, what you can do is use view models. Essentially, you just create a class that has some or all of the properties from your entity, usually named after the entity with something like VM
or ViewModel
tacked on to the end. So in this scenario, that might be TipoSensoresViewModel
. Then, you can totally customize the display name for that class' nombre
property, and use this for your view instead of your entity.
The only missing piece is getting the data from the entity into your view model. You can either manually map the value:
var model = new TipoSensoresViewModel {
nombre = tipoSensoresInstance.nombre
};
Or use a library like AutoMapper.
Upvotes: 2