Bonjour.
Ci-dessous mon code c# correspondant à mon modèle:
Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class LivrePapier { public string Titre { get; set; } public float Prix { get; set; } public int IdLivre { get; set; } public List<Format> Formats { get; set; } } public class Format { public int Id { get; set; } public string Label { get; set; } public bool? Selected { get; set; } public string Description { get; set; } }
Ci-dessous mon code c# correspondant à mon controller "GestionLivreController.cs":
Code C# : 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 public ActionResult Index() { LivrePapier livpaper = new LivrePapier(); List<Format> listform = new List<Format>(); Format One = new Format(); Format Two = new Format(); Format Three = new Format(); One.Label = "Test1"; One.Description = "1Test"; One.Selected = true; Two.Label = "Test2"; Two.Description = "2Test"; Two.Selected = false; Three.Label = "Test3"; Three.Description = "3Test"; Three.Selected = true; listform.Add(One); listform.Add(Two); listform.Add(Three); livpaper.Titre = "Tests"; livpaper.Prix = "2"; livpaper.Formats = listform; return PartialView("vuedeschoix", livpaper); } public JsonResult Save(LivrePapier data) { //code de traitement return Json(new { message = "Enregistrement terminé"}); }
Ci-dessous mon code cshtml (razor) correspondant à ma vue "vuedeschoix.cshtml":
Code cshtml : 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
35
36
37 @model namespace.LivrePapier @using (Html.BeginForm("Save", "GestionLivre")) { @Html.ValidationSummary(true) <table> <tr> <th>Exemple</th> <th>Sélectionner le format</th> <th>Autre Test</th> </tr> <tr> <td>Exemple1</td> <td> @Html.CheckBoxFor(m => m.listform.Find(x => x.Label.Contains("Test1")).Selected)</td> <td> @Html.TextBoxFor(m => m.listform.Find(x => x.Description.Contains("1Test")).Description) </td> </tr> <tr> <td>Exemple2</td> <td> @Html.CheckBoxFor(m => m.listform.Find(x => x.Label.Contains("Test2")).Selected)</td> <td> @Html.TextBoxFor(m => m.listform.Find(x => x.Description.Contains("2Test")).Description) </td> </tr> <tr> <td>Exemple3</td> <td> @Html.CheckBoxFor(m => m.listform.Find(x => x.Label.Contains("Test3")).Selected)</td> <td> @Html.TextBoxFor(m => m.listform.Find(x => x.Description.Contains("3Test")).Description) </td></tr> </table> <input type="submit" value="Créer" /> <input type="reset" value="Annuler" /> }
Or, quand je clique sur le bouton "Créer" pour envoyer le formulaire, je constate (en mode debug) que l'argument 'data' (de type LivrePapier) de la méthode Save du controller contient une liste vide pour la liste Formats (List<Format>).
Pourtant la liste 'listform' a bien été utilisé.
Pourriez-vous, s'il vous plait, m'aider à résoudre ce problème?
Merci.
Partager