private void FillSelect(Viking viking) { TypeVikingFactory typeVikingFactory = new TypeVikingFactory(db); ViewBag.ID_TYPE = new SelectList(typeVikingFactory.GetAll(), "Id", "TypeVikingId", viking.TypeViking); //Chargement d'une liste vide à la création TypeVikingFactory sousTypeFactory = new TypeVikingFactory(db); ViewBag.ID_SOUS_TYPE = new SelectList(sousTypeFactory.getManyBy(viking.TypeVikingId).OrderBy(i => i.Value), "Key", "Value", viking.TypeViking); }
//Mise à jour Ajax de la liste imbriquée public JsonResult listeSousType(int Id, int?defaultSelected) { TypeVikingFactory sousTypeFactory = new TypeVikingFactory(db); List <SelectListItem> listType = new List <SelectListItem>(); Dictionary <int, string> infoType = sousTypeFactory.getManyBy(Id); //le dictionnaire doit devenir une liste de types List <Tuple <int, string> > listeType = new List <Tuple <int, string> >(); foreach (var type in infoType) { listeType.Add(new Tuple <int, string>(int.Parse(type.Key.ToString()), type.Value)); } foreach (Tuple <int, string> liste in listeType) { listType.Add(new SelectListItem { Text = liste.Item2, Value = liste.Item1.ToString() }); } return(Json(new SelectList(listType, "Value", "Text", defaultSelected), JsonRequestBehavior.AllowGet)); }