我在我的 SQL Server
中创建了以下表格:
Create Table tblDepartment(
Id int primary key identity,
Name nvarchar(50)
)
Create table tblEmployee(
EmployeeId int primary key identity,
Name nvarchar(50) not null,
Gender nvarchar(10) not null,
City nvarchar(50),
DepartmentId int not null foreign key references tblDepartment(Id)
)
如您所见,
DepartmentId
tblEmployee
中的列是tblDepartment
的外键Id
柱子。是一对多关系:eachDepartment
可以有很多Employee
- 两者
tblDepartment
和tblEmployee
有Name
栏目
然后在我的 MVC
应用程序,使用两个表,我自动生成以下EmployeeDataModel.edmx
:
(请注意,我将名称 tblEmployee
替换为 Employee
,并将 tblDepartment
替换为 Department
)
当然,我有以下自动生成的实体类:
public partial class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
public partial class Department
{
public Department()
{
this.Employees = new HashSet<Employee>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
现在,主要的<table>
在我的Employee/Index.cshtml
View
看起来像这样:
... using here
@model IEnumerable<Employee>
...
<table class="table" border="1">
<tr>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.Gender)</th>
<th>@Html.DisplayNameFor(model => model.City)</th>
<th>@Html.DisplayNameFor(model => model.Department.Name)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>@Html.DisplayFor(modelItem => item.Gender)</td>
<td>@Html.DisplayFor(modelItem => item.City)</td>
<td>@Html.DisplayFor(modelItem => item.Department.Name)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.EmployeeId }) |
@Html.ActionLink("Details", "Details", new { id = item.EmployeeId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.EmployeeId })
</td>
</tr>
}
</table>
请注意,在表标题中,我有两个“名称”:
<th>@Html.DisplayNameFor(model => model.Name)</th>
....
<th>@Html.DisplayNameFor(model => model.Department.Name)</th>
两者都会显示“名称”,与 tblEmployee
相符和tblDepartment
数据库中的真实列名。我想避免这种情况,所以我创建了一个 partial class
对于Department
显示tblDepartment
Name
如Department Name
:
[MetadataType(typeof(DepartmentMetaData))]
public partial class Department {
}
public class DepartmentMetaData {
[Display(Name="Department Name")]
public string Name { get; set;}
}
这次成功了:
但是现在,我有另一个 View
:Employee/Create.cshtml
:
<div class="form-horizontal">
<h4>Employee</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Gender", new List<SelectListItem> {
new SelectListItem {Text="Male", Value="Male"},
new SelectListItem{Text="Female", Value="Female"}
}, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DepartmentId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
请注意,我有一个字段 DepartmentId
与 DropdownList
:
<div class="form-group">
@Html.LabelFor(model => model.DepartmentId, "DepartmentId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("DepartmentId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DepartmentId, "", new { @class = "text-danger" })
</div>
</div>
就像我的Employee/Index.cshtml
一样View
,这一次,我不想显示DepartmentId
Label
如"DepartmentId"
,因此我创建了另一个 partial class
对于 Employee
显示"DepartmentId"
如"Department Name"
:
[MetadataType(typeof(EmployeeMetaData))]
public partial class Employee {
}
public class EmployeeMetaData {
[Required]
public string Name { get; set; }
[Required]
public string Gender { get; set; }
[Required]
public string City { get; set; }
[Required]
[Display(Name="Department Name")] //here it is
public int DepartmentId { get; set; }
}
但是渲染后,我的 Employee/Create.cshtml
View
仍然显示DepartmentId
作为其 Label
:
为什么会这样呢?如何显示Label
如Department Name
也是吗?
注:全部Views
( Index
和 Create
)是在我创建 EmployeeController
时自动生成的带脚手架选项:MVC5 Controller with views using Entity Framework
我使用Entity Framework 6.1.1
和VS2013
如果它们重要的话。
最佳答案
@Html.LabelFor(model => model.DepartmentId, "DepartmentId",
htmlAttributes: new { @class = "control-label col-md-2" })
应该是
@Html.DisplayNameFor(model => model.DepartmentId,
htmlAttributes: new { @class = "control-label col-md-2" })
或者
@Html.LabelFor(model => model.DepartmentId,
htmlAttributes: new { @class = "control-label col-md-2" })
关于c# - 如何更改View中的显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338598/