Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改视图中的显示文字?_C#_Asp.net_Sql Server_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何更改视图中的显示文字?

C# 如何更改视图中的显示文字?,c#,asp.net,sql-server,asp.net-mvc,entity-framework,C#,Asp.net,Sql Server,Asp.net Mvc,Entity Framework,我正在SQL Server中创建以下表: 如你所见 tblEmployee中的DepartmentId列是tblDepartment Id列的外键。这是一对多关系:每个部门可以有许多员工 tblDepartment和tblEmployee都有Name列 然后在我的MVC应用程序中,使用这两个表,我自动生成以下EmployeeDataModel.edmx: 注意,我用Employee替换tblEmployee,用Department替换tblDepartment 当然,我有以下自动生成的实体类:

我正在SQL Server中创建以下表:

如你所见

tblEmployee中的DepartmentId列是tblDepartment Id列的外键。这是一对多关系:每个部门可以有许多员工 tblDepartment和tblEmployee都有Name列 然后在我的MVC应用程序中,使用这两个表,我自动生成以下EmployeeDataModel.edmx:

注意,我用Employee替换tblEmployee,用Department替换tblDepartment

当然,我有以下自动生成的实体类:

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; }
}
请注意,在表标题中,我有两个名称:

<th>@Html.DisplayNameFor(model => model.Name)</th>
....
<th>@Html.DisplayNameFor(model => model.Department.Name)</th>
这一次是成功的:

但现在,我有了另一个视图:Employee/Create.cshtml:

与我的Employee/Index.cshtml视图一样,这次我不想将DepartmentId标签显示为DepartmentId,因此我为Employee创建了另一个分部类,将DepartmentId显示为Department Name:

但呈现时,my Employee/Create.cshtml视图仍将DepartmentId显示为其标签:

为什么会这样?如何将标签也显示为部门名称

注意:当我使用支架选项创建EmployeeController时,所有视图索引和创建都是自动生成的:使用实体框架创建视图的MVC5控制器

如果实体框架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" })

下拉控件的LabelFor有一个额外的参数DepartmentId,用于覆盖显示名称。移除这些应该会起作用

<div class="form-group">
    @Html.LabelFor(model => model.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>

使用dataannotations显示正确的数据names@rashfmnb我补充说,在我的部分课程中,对于员工和部门来说,问题仍然存在。有什么想法吗?我不明白的是,当我为我的部门属性名称添加Display属性时,它在索引视图中运行良好,但是,即使在我为我的员工属性DepartmentId添加了显示属性之后,它也无法在我的创建视图中工作。您是否尝试了@Html.DisplayFormodelItem=>item.DepartmentId而不是@Html.LabelFormodel=>model.DepartmentId,DepartmentId,htmlAttributes:new{@class=控件标签col-md-2}你不是在用Html.LabelFor的第二个参数制作DepartmentId吗?@thepanch,Cetin Basoz你搞定了。我使用脚手架生成它,所以我错过了检查。谢谢。这是正确的答案。我是通过脚手架自动生成的,所以我没有意识到。谢谢
<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>
    <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>
[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; }
}
@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" })
<div class="form-group">
    @Html.LabelFor(model => model.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>