我有 Employee
,其中 gender_id 是 FK 到 Gender
表。并使用 EF 生成类来访问数据库。
我创建了一个元数据类型,这样我就可以更改页面上的标签显示。
[MetadataType(typeof(EmployeeMetaData))]
public partial class Employee
{
}
public class EmployeeMetaData
{
[Display(Name = "Nombre2")]
public string first_name { get; set; }
[Display(Name = "Apellido")]
public string last_name { get; set; }
[Display(Name = "Sexo")]
public Nullable<int> gender_id { get; set; }
}
但是 gender_id
并没有像您在图片上看到的那样改变。
所以我认为还需要为 Gender
创建一个元数据,但仍然没有显示在页面上。
这是自动生成的 View ,last_name
可以,但是 gender_id
不行
<div class="form-group">
@Html.LabelFor(model => model.last_name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.last_name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.last_name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.gender_id, "gender_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("gender_id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.gender_id, "", new { @class = "text-danger" })
</div>
</div>
最佳答案
将自动生成的 View 代码添加到问题后,意识到问题是 LabelFor
还包括标签的名称。
@Html.LabelFor(model => model.gender_id,
"gender_id",
htmlAttributes: new { @class = "control-label col-md-2" }
)
删除 "gender_id"
LabelFor 后开始使用 MetaData 别名。
Of course this will bring the problem if I autogenerate the view again, the changes will be lost.
关于c# - 如何更改外键的显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48503662/