Bonjour,
Je vous explique mon problème. J'ai 2 entités ( Instructor et OfficeAssignement) .
Ce que je veux c'est pouvoir lister tous les éléments de la classe Instructor avec la location. Cela marche parfaitement en utilisant la propriété de navigation OfficeAssignement grâce à laquelle je récupère la Location.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class OfficeAssignement { [Key] [ForeignKey("Instructor")] public int InstructorID { get; set; } public string Location { get; set; } public virtual Instructor Instructor { get; set; } } public class Instructor { [Display(Name = "Full Name")] public string FullName { get { return FirstName + " " + LastName; } } [Key] public int InstructorID { get; set; } public string LastName { get; set; } public string FirstName { get; set; } [Display(Name = "Date of hire")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0: yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime HireDate { get; set; } public ICollection<Course> Courses { get; set; } public OfficeAssignement OfficeAssignement { get; set;} public ICollection<Department> Departments { get; set; } }
Dans la vue de l'instructor, je mets :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.OfficeAssignement.Location) </th> <th> @Html.DisplayNameFor(model => model.FullName) </th> <th> @Html.DisplayNameFor(model => model.HireDate) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.OfficeAssignement.Location) </td> <td> @Html.DisplayFor(modelItem => item.FullName) </td> <td> @Html.DisplayFor(modelItem => item.HireDate) </td>
En revanche : quand je veux éditer, rien ne se passe. En effet, j'ai toujours eu l'habitude d'utiliser des clés étrangères ( grâce à par exemple une locationID qui aurait pu se trouver dans l'entité Instructor).
Donc j'arrive bien à récupérer les données mais pas à mettre à jour.
Voici mon code pour l'instant dans l'action Edit du controler Instructor:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include="InstructorID,LastName,FirstName,HireDate")] Instructor instructor) { if (ModelState.IsValid) { db.Entry(instructor).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.InstructorID = new SelectList(db.OfficeAssignements, "InstructorID", "Location", instructor.InstructorID); return View(instructor); }
Voici pour l'instant mon code dans la vue Edit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 <div class="form-group"> @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.HireDate, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.HireDate) @Html.ValidationMessageFor(model => model.HireDate) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.OfficeAssignement, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.OfficeAssignement) @Html.ValidationMessageFor(model => model.OfficeAssignement) </div> </div>
Est-ce que je suis obligé de créer une clé étrangère ( par ex locationId au sein de l'entité Instructor) ?
Ou alors est-il possible de tout faire avec une propriété de navigation ?
Merci beaucoup pour votre aide
Partager