Bonjour,
Suivant mon code ci-dessous, j'arrive pas a faire affiché ce qui est selectionner dans un dropdownlist. Pouvez-vous m'aider svp?
J'obtiens toujours comme reponse : System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]
Merci.
Voici le modele:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 public class Personne { [Display(Name = "Nom")] public string NomMembre { get; set; } [Display(Name ="Prénom")] public string PrenomMembre { get; set; } [Display(Name = "Sexe")] public IEnumerable<SelectListItem> SexeMembre { get; set; }
Voici le controller:
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 public ActionResult Personne() { Personne p = new Models.Personne(); List<SelectListItem> sexe = new List<SelectListItem>(); sexe.Add(new SelectListItem() { Text = "Féminin", Value = "F" }); sexe.Add(new SelectListItem() { Text = "Masculin", Value = "M" }); ViewBag.Sex = sexe; return View(); } // Retourne la reponse sur la meme vue [HttpPost] public ActionResult Personne(Personne personne) { string codeMembre; Random rnd = new Random(); int a = rnd.Next(999); codeMembre = personne.NomMembre.Substring(0, 3) + a + personne.PrenomMembre.Substring(0, 3); personne.Code = codeMembre; ViewData["donne"] = personne.SexeMembre return View(personne); }
Voici la vue:
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
35
36
37
38
39
40
41
42
43
44
45
46 @model Mvctraining.Models.Personne @{ ViewBag.Title = "Personne"; } <h2>Membre</h2> @using (Html.BeginForm("Personne", "Home")) { <table> <tr> <th>@Html.LabelFor(model => model.NomMembre)</th> <th>@Html.EditorFor(model => model.NomMembre)</th> </tr> <tr> <th>@Html.LabelFor(model => model.PrenomMembre)</th> <th>@Html.EditorFor(model => model.PrenomMembre)</th> </tr> <tr> <th>@Html.LabelFor(model =>model.SexeMembre)</th> <th>@Html.DropDownListFor(model => model.SexeMembre, (IEnumerable<SelectListItem>)ViewBag.Sex, "Selectionar", new { @class = "form-control" }) </th> </tr> <tr> <th></th> <th><input type="submit" value="Valider" onclick="location.href'@Url.Action("Personne", "Home")'" /></th> </tr> </table> } <section> <article><br /> @ViewData["donne"] </article> </section>
Partager