Bonjour,

je souhaite initialiser l'id et le nom d'un model de classe, en l'occurrence Auteur en sélectionnant un nom d'auteur avec le DropDownList

@Html.DropDownListFor.

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
 public ActionResult Livre()
        {
 
            List<Auteur> listeDesAuteurs = dal.ObtientTousLesLivres().Select(lv => lv.Auteur).GroupBy(x => x.Nom).Select(x => x.First()).OrderBy(r => r.Nom).ToList();
            ViewBag.ListeDesAuteurs = listeDesAuteurs;
            if (listeDesAuteurs == null)
                return View("Error");
            return View();
 
        }
        [HttpPost]
        public ActionResult Livre(Livre livre)
        {
            if (dal.LivreExiste(livre.Titre))
            {
                ModelState.AddModelError("Titre", "Ce titre de livre existe déjà");
                return View(livre);
            }
            if (!ModelState.IsValid)
                return View(livre);
            dal.CreerLivre(livre.Titre,livre.DateParution,livre.Auteur.Nom);
            return RedirectToAction("~/Views/Afficher/Index.cshtml");
       }
Voici le 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
38
39
40
41
42
43
@model Bibliotheque.Models.Livre
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Livre</title>
    <link type="text/css" href="../../Content/Site.css" rel="stylesheet" />
    <script type="text/javascript" src="../../Scripts/jquery-3.4.1.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.validate-vsdoc.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.validate.unobtrusive.js"></script>
</head>
<body>
    @using (Html.BeginForm())
    {
        <fieldset>
            <legend>Ajouter un livre</legend>
 
            <div>
                @Html.LabelFor(model => model.Titre)
                @Html.TextBoxFor(model => model.Titre)
                @Html.ValidationMessageFor(model => model.Titre)
            </div>
            <div>
                @Html.LabelFor(model => model.DateParution)
                @Html.EditorFor(model => model.DateParution, new { @type = "date" })
                @Html.ValidationMessageFor(model => model.DateParution)
            </div>
            <div>
                @*@Html.DropDownList("AuteurChoisi", (SelectList)ViewBag.ListeDesAuteurs)*@
                @Html.DropDownListFor(model => model.Auteur.Id, new SelectList(ViewBag.ListeDesAuteurs, "Id", "Nom"), "Selectionné un Auteur")
            </div>
 
                <br />
                <input type="submit" value="Ajouter" />
</fieldset>
                    }
</body>
</html>

Enfin la classe Livre:

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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
 
namespace Bibliotheque.Models
{
    public class Livre
    {
        public int Id { get; set; }
 
        [Required]
        public string Titre { get; set; }
 
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DateParution { get; set; }
 
        public virtual Auteur Auteur { get; set; }
 
    }
}
et la classe Auteur:

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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
 
namespace Bibliotheque.Models
{
    public class Auteur
    {
        public int Id { get; set; }
 
        [Required]
        public string Nom { get; set; }
    }
}
Le problème, c'est que seul l'Id de la classe Auteur est initialisé, lorsque je sélectionne avec la DropDownListFor et je voudrais que cela initialise en même temps le nom de l'auteur.

Merci d'avance.

Mumu27!