// GET: Formation public ActionResult ToutesLesFormations() { var vm = new AccueilViewModel(); //using -->variable de contexte détruite après utilisation using (var context = new AvisEntities()) { var listFormation = context.Formation.OrderBy(x => Guid.NewGuid()).ToList(); foreach (var f in listFormation) { var dto = new FormationAvecAvisDto(); dto.Formation = f; if (f.Avis.Count == 0) { dto.Note = 0; } else { dto.Note = Math.Round(f.Avis.Average(a => a.Note), 2); } vm.ListFormations.Add(dto); } } return(View(vm)); }
// GET: Formation public ActionResult ToutesLesFormations() { //List<Formation> listFormations = new List<Formation>(); //using (var context = new AvisEntities()) //{ // listFormations = context.Formation.ToList(); //} ////context.Dispose(); //return View(listFormations); // var vm = new AccueilViewModel(); using (var context = new AvisEntities()) { //var listFormation = new List<Formation>(); //context.Formation.Take(4).ToList(); var listFormation = context.Formation.OrderBy(x => Guid.NewGuid()).ToList(); foreach (var f in listFormation) { var dto = new FormationAvecAvisDto(); dto.Formation = f; if (f.Avis.Count == 0) { dto.Note = 0; } else { dto.Note = Math.Round(f.Avis.Average(a => a.Note), 2); } vm.ListFormations.Add(dto); } } return(View(vm)); }
public ActionResult Accueil() { var vm = new AccueilViewModel(); using (var context = new AvisEntities()) { var listFormation = context.Formation.OrderBy(x => Guid.NewGuid()).Take(4).ToList(); foreach (var f in listFormation) { var dto = new FormationAvecAvisDto(); dto.Formation = f; if (f.Avis.Count == 0) { dto.Note = 0; } else { dto.Note = Math.Round(f.Avis.Average(a => a.Note), 2); } vm.ListFormations.Add(dto); } } return(View(vm)); }