Browse Source

ViewModels: permet de passer le dbContext au constructeur du ViewModel

olivier.massot 7 years ago
parent
commit
84e03e3687

+ 6 - 6
CD67.FicheCollege.MVC/Controllers/CollegesController.cs

@@ -38,7 +38,7 @@ namespace CD67.FicheCollege.MVC.Controllers
                 return HttpNotFound();
             }
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Lecture);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Lecture);
 
             return View(model);
         }
@@ -53,7 +53,7 @@ namespace CD67.FicheCollege.MVC.Controllers
             Entity.College college = new Entity.College();
             college.Annee_Id = annee_id.Value;
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Creation);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
 
             return View("Edit", model);
         }
@@ -72,7 +72,7 @@ namespace CD67.FicheCollege.MVC.Controllers
                 return RedirectToAction("Details", new { id = college.Id });
             }
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Creation);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Creation);
 
             return View("Edit", model);
         }
@@ -87,7 +87,7 @@ namespace CD67.FicheCollege.MVC.Controllers
                 return HttpNotFound();
             }
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Modification);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
 
             return View(model);
         }
@@ -106,7 +106,7 @@ namespace CD67.FicheCollege.MVC.Controllers
                 return RedirectToAction("Details", new { Id = college.Id });
             }
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Modification);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Modification);
 
             return View(model);
         }
@@ -125,7 +125,7 @@ namespace CD67.FicheCollege.MVC.Controllers
                 return HttpNotFound();
             }
 
-            CollegeViewModel model = new CollegeViewModel(college, ModeAcces.Suppression);
+            CollegeViewModel model = new CollegeViewModel(college, db, ModeAcces.Suppression);
 
             return View("Details", model);
         }

+ 15 - 2
CD67.FicheCollege.MVC/Models/BaseViewModel.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using CD67.FicheCollege.Entity;
+using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 
 namespace CD67.FicheCollege.MVC.Models
@@ -34,7 +35,7 @@ namespace CD67.FicheCollege.MVC.Models
         public UtilisateurConnecte User;
 
         // ***************************
-        // Constructeur de base
+        // Constructeurs de base
         public BaseViewModel(T model,
             ModeAcces acces = ModeAcces.Lecture,
             Dictionary<string, object> bag = null)
@@ -46,5 +47,17 @@ namespace CD67.FicheCollege.MVC.Models
             User = Internal.UtilisateurConnecteFactory.getUtilisateurConnecte();
         }
 
+        public BaseViewModel(T model,
+            Entities dbContext,
+            ModeAcces acces = ModeAcces.Lecture,
+            Dictionary<string, object> bag = null)
+        {
+            Obj = model;
+            Acces = acces;
+            if (bag != null)
+                Bag = bag;
+            User = Internal.UtilisateurConnecteFactory.getUtilisateurConnecte();
+        }
+
     }
 }

+ 2 - 3
CD67.FicheCollege.MVC/Models/CollegeViewModel.cs

@@ -11,12 +11,11 @@ namespace CD67.FicheCollege.MVC.Models
         public SelectList Sel_TypesCollege;
         public SelectList Sel_CodesPostaux;
 
-        public CollegeViewModel(College model, ModeAcces acces = ModeAcces.Lecture, Dictionary<string, object> bag = null) : base(model, acces, bag)
+        public CollegeViewModel(College model, Entities dbContext, ModeAcces acces = ModeAcces.Lecture, Dictionary<string, object> bag = null) : base(model, acces, bag)
         {
             if (acces == ModeAcces.Creation | acces == ModeAcces.Modification)
             {
-                Entities db = new Entities();
-                TypeCollegeFactory fact = new TypeCollegeFactory(db);
+                TypeCollegeFactory fact = new TypeCollegeFactory(dbContext);
                 Sel_TypesCollege = new SelectList(fact.getAll(), "Id", "Libelle", Obj.TypeCollege_Id);
                 Sel_CodesPostaux = new SelectList(Referentiel.GetCodesPostaux(Obj.Commune_Insee), Obj.Code_Postal);
             }