C# 更改外键MVC的显示名称
在我的MVC应用程序中,我有一个属于特定“类别”的型号,另一个型号。C# 更改外键MVC的显示名称,c#,asp.net-mvc,asp.net-mvc-5,entity-framework-6,C#,Asp.net Mvc,Asp.net Mvc 5,Entity Framework 6,在我的MVC应用程序中,我有一个属于特定“类别”的型号,另一个型号。 当显示索引视图时,此模型的名称和类别名称都称为“名称”,如何将类别的显示名称更改为名称以外的名称 [Table("Product")] public partial class Product { public int ProductId { get; set; } [Required] public string Name { get; set; }
当显示索引视图时,此模型的名称和类别名称都称为“名称”,如何将类别的显示名称更改为名称以外的名称
[Table("Product")]
public partial class Product
{
public int ProductId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public int CategoryId { get; set; }
public decimal Price { get; set; }
public virtual Category Category { get; set; }
}
我尝试过在CategoryId和Category上面使用[DisplayName(“新名称”)]
,但这似乎不起作用,有人能建议我吗
谢谢当您使用脚手架生成视图时,可能会发生这种情况。它发生在使用下拉列表选择外键的地方。与其他生成的标签不同,这些标签传递了一个附加参数“labelText”,该参数将覆盖在模型属性中设置的显示名称。删除附加参数或将其更改为要显示的文本。我在下面的示例代码中添加了文本(ChangeOrRemoveMe)
<div class="form-group">
@Html.LabelFor(model => model.CategoryID, "CategoryID(ChangeOrRemoveMe)", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryID, "", new { @class = "text-danger" })
</div>
</div>
@Html.LabelFor(model=>model.CategoryID,“CategoryID(changeorremovem)”,htmlAttributes:new{@class=“control label col-md-2”})
@DropDownList(“CategoryID”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoryID,“,new{@class=“text danger”})
您正在使用任何其他映射文件吗?不清楚您想要什么,但是如果您使用[Display(Name=“New Name”)]public int CategoryId{get;set;}`并且在视图@Html.LabelFor(m=>m.CategoryId)
中,它将呈现“New Name”