Ich habe die folgenden Tabellen in meiner SQL Server
erstellt:Wie kann der Anzeigetext in der Ansicht geändert werden?
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)
)
Wie Sie sehen können,
- Die
DepartmentId
Spalte in dertblEmployee
ist der Fremdschlüssel für dietblDepartment
Id
Spalte. Es ist eine Eins-zu-viele-Beziehung: jederDepartment
vieleEmployee
- Sowohl
tblDepartment
undtblEmployee
Name
Spalte
Dann in meiner MVC
Anwendung haben haben kann, die beiden Tabellen, ich folgendes EmployeeDataModel.edmx
automatisch generieren:
(Beachten Sie, dass ich den Namen tblEmployee
mit Employee
ersetzen und tblDepartment
mit Department
)
Und natürlich, ich habe die folgenden automatisch generierten Entitätsklassen:
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; }
}
Nun sieht die Haupt <table>
in meinem Employee/Index.cshtml
View
wie folgt aus:
... 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>
Beachten Sie, dass In den Tabellenköpfen habe ich zwei "Name" s:
<th>@Html.DisplayNameFor(model => model.Name)</th>
....
<th>@Html.DisplayNameFor(model => model.Department.Name)</th>
Das würde sowohl "Name" anzeigen, passen mit den realen Spaltennamen tblEmployee
und tblDepartment
in der Datenbank.Ich möchte, dass vermeiden, so dass ich erstellen partial class
für die Department
die tblDepartment
Name
als Department Name
anzuzeigen:
[MetadataType(typeof(DepartmentMetaData))]
public partial class Department {
}
public class DepartmentMetaData {
[Display(Name="Department Name")]
public string Name { get; set;}
}
Und dieses ist erfolgreich:
Aber jetzt, ich habe ein anderes 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>
Und beachten Sie, dass ich ein Feld für DepartmentId
mit dem DropdownList
haben:
<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>
Genau wie für meine Employee/Index.cshtml
View
, diesmal will ich nicht DepartmentId
Label
als "DepartmentId"
angezeigt werden, so schaffe ich eine andere partial class
für Employee
zu Anzeige "DepartmentId"
als "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; }
}
Aber wenn gemacht, mein Employee/Create.cshtml
View
zeigt noch DepartmentId
als Label
:
Warum ist das so? Wie kann ich die Label
als Department Name
auch anzeigen?
Hinweis: alle Views
(Index
und Create
) werden automatisch generiert, wenn ich EmployeeController
mit Baugerüst Option erstellen: MVC5 Controller with views using Entity Framework
Ich benutze Entity Framework 6.1.1
und VS2013
wenn sie wichtig sind.
Verwendung DataAnnotations zum Anzeigen von Eigennamen – rashfmnb
@rashfmnb Ich fügte hinzu, dass in meinem 'Teil CLASS', sowohl für' Employee' und 'Department' das Problem weiterhin besteht. Irgendeine Idee? Was ich nicht verstehe ist, dass es in meinem 'Index'' View' funktioniert (wenn ich 'Display' Attribut für meine' Department' Eigenschaft 'Name' hinzufüge), aber es funktioniert nicht in meiner' Create'' Ansicht '(selbst nachdem ich das' Display' Attribut für meine 'Employee' Eigenschaft' DepartmentId' hinzugefügt habe) – Ian
hast du versucht '@ Html.DisplayFor (modelItem => item.DepartmentId)' statt '' Html.LabelFor (model => model.DepartmentId, "DepartmentId", htmlAttributes: new {@class = "Kontroll-Label col-md-2"}) ' – thepanch