Comment Remplir et afficher les Entity Framework dans un DropDownList
Bonjour :D
Dans le même modèle j'ai :
4 tables qui contiennent des donnés simples
Table 1 : "Trcube1"
Table 2 : "Trcube2"
Table 3 : "Trcube3"
et une table "ListeDesTrCube" qui a deux colonne (id, NameTable) qui contient les noms de mes 3 tables précédentes c'est-à-dire comme ci
IdTable:
1
2
3
NameTable
Trcube1
Trcube2
Trcube3
Le but :
Ce que je veux faire c'est de charger les donnée de ma table "ListeDesTrCube" c-à-d les noms des tables(Trcube1,Trcube2 etTrcube3) dans une liste déroulante (DropDownList) et quand je sélectionne un nom d'une table, les donnée de cette dernière s'affiche
Déjà fait :
Jusqu'à maintenant j'arrive a charger les donnée de ma table "ListeDesTrCube" dans une liste déroulante (DropDownList)
voila mon controller
Code:
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| namespace WebPortal.Models.Cubes.Axes //++
{
public class SaisieReferentielController : Controller
{
/// <summary>
/// Affichage de la liste des Axe.
/// </summary>
/// <param name="appli"></param>
/// <returns></returns>
public ActionResult Index()
{
AntPilOscarEntities dbListeAxeEntites = new AntPilOscarEntities();
/*Getting data from database*/
List<LISTEAXES> QueryGetlistAxes = (from data in dbListeAxeEntites.LISTEAXES
select data).ToList();
LISTEAXES axe = new LISTEAXES();
axe.LIBELLE = "Choisir un axe à modifier";
axe.IDAXE = 0;
QueryGetlistAxes.Insert(0, axe);
SelectList selectListeAxe = new SelectList(QueryGetlistAxes, "IDAXE", "LIBELLE", 0);
/*Assign value to model*/
ListeAxeModel axeModel = new ListeAxeModel();
axeModel.SelectListAxe = selectListeAxe;
return View(axeModel);
}
[HttpPost]
public ActionResult Index(int ddlcountry)
{
AntPilOscarEntities dbListeAxeEntites = new AntPilOscarEntities();
/*Getting data from database*/
List<LISTEAXES> QueryGetlistAxes = (from data in dbListeAxeEntites.LISTEAXES
select data).ToList();
LISTEAXES axe = new LISTEAXES();
axe.LIBELLE = "Choisir un axe à modifier";
axe.IDAXE = 0;
QueryGetlistAxes.Insert(0, axe);
SelectList selectListeAxe = new SelectList(QueryGetlistAxes, "IDAXE", "LIBELLE", 0);
/*Assign value to model*/
ListeAxeModel axeModel = new ListeAxeModel();
axeModel.SelectListAxe = selectListeAxe;
/*Obtenir l'axe a modifier */
ViewBag.AxeName = QueryGetlistAxes.Where(m => m.IDAXE == ddlcountry).FirstOrDefault().LIBELLE;
return View(axeModel);
}
}
} |
Voila mon model de donnée
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using WebPortal.DataBase.Utils;
using WebPortal.DataBase.Cubes;
using PagedList;
namespace WebPortal.Models.Cubes.Axes
{
public class ListeAxeModel
{
public SelectList SelectListAxe { get; set; }
public IPagedList<LISTEAXES> Axe { get; set; }
public ChoisirAxeAModifier ChoixAxe { get; set; }
}
} |
Voila l'index html
Code:
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
| @model WebPortal.Models.Cubes.Axes.ListeAxeModel
<script src="@Url.Content("~/Scripts/daypilot-modal-2.0.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/ConfirmationSuppression.js")" type="text/javascript"></script>
@{
ViewBag.Title = "SaisieReferentiel";
Layout = "~/Views/Shared/_PortalLayout.cshtml";
}
<h2>Gestion du référentiel des cubes Antenne</h2>
@using (Html.BeginForm("Index", "SaisieReferentiel"))
{
<table>
<tr>
<td>
@Html.LabelFor(val => Model.ChoixAxe.IdAxe) :
</td>
<td>
@Html.DropDownList("ddlcountry", Model.SelectListAxe, new { @style = "width:200px;" })
</td>
</tr>
<p>
@Html.ActionLink("Créer un nouvel enregistrement", "Create")
</p>
</table>
<input type="submit" value="Afficher l'Axe" />
<div>
Selected :@ViewBag.AxeName</div>
} |
Reste à faire :
Quand je sélectionne un nom d'une table, les donnée de cette dernière s'affiche en bas :(
Merci à tous