c# - 如何更改View中的显示文本?

标签 c# asp.net sql-server asp.net-mvc entity-framework

我在我的 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)
)

如您所见,

  1. DepartmentId tblEmployee 中的列是 tblDepartment 的外键Id柱子。是一对多关系:each Department可以有很多Employee
  2. 两者tblDepartmenttblEmployeeName栏目

然后在我的 MVC应用程序,使用两个表,我自动生成以下EmployeeDataModel.edmx :

enter image description here

(请注意,我将名称 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 NameDepartment Name :

[MetadataType(typeof(DepartmentMetaData))]
public partial class Department {
}

public class DepartmentMetaData {
    [Display(Name="Department Name")]
    public string Name { get; set;}
}

这次成功了:

enter image description here

但是现在,我有另一个 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>

请注意,我有一个字段 DepartmentIdDropdownList :

    <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 :

enter image description here

为什么会这样呢?如何显示LabelDepartment Name也是吗?

注:全部Views ( IndexCreate )是在我创建 EmployeeController 时自动生成的带脚手架选项:MVC5 Controller with views using Entity Framework

我使用Entity Framework 6.1.1VS2013如果它们重要的话。

最佳答案

@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/

相关文章:

asp.net - 在 ASP.Net 标记中显示字符串变量的值

c# - DateTime 在 SQL Server 中不起作用

sql - 选择顶行和底行

c# - 拖放选定的项目作为列表框返回null

c# - ServiceStack授权-访问路由信息

C#保护数据库连接信息

c# - log4net AdoNetAppender 在 Application_Start() 中不工作

c# - Mathf.PingPong 速度问题?

javascript - 检查asp:FileUpload控件是否在JavaScript中选择了文件

sql - SQL Server 中的组合