Преглед изворни кода

Redirection Home > Année en cours

olivier.massot пре 7 година
родитељ
комит
8f76a3ad34

+ 1 - 0
CD67.FicheCollege.Entity/CD67.FicheCollege.Entity.csproj

@@ -83,6 +83,7 @@
       <DesignTime>True</DesignTime>
       <DependentUpon>EntityModel.edmx</DependentUpon>
     </Compile>
+    <Compile Include="Extend\Annee.cs" />
     <Compile Include="Extend\ActionCLAS.cs" />
     <Compile Include="Extend\College.cs" />
     <Compile Include="Extend\Territoire.cs" />

+ 35 - 0
CD67.FicheCollege.Entity/Extend/Annee.cs

@@ -0,0 +1,35 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+
+namespace CD67.FicheCollege.Entity
+{
+    /// <summary>
+    /// Classe d'extension de celle d'Entity, nécessaire pour y associer les Metadata
+    /// </summary>
+    [MetadataType(typeof(Annee_Metadata))]
+    public partial class Annee
+    {
+        public static int get_current_year_id()
+        {
+            Entities db = new Entities();
+            string annee_lib;
+            if (DateTime.Now.Month <= 7)
+            {
+                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year - 1, DateTime.Now.Year);
+            }
+            else
+            {
+                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year, DateTime.Now.Year + 1);
+            }
+            return db.Annees.Where(a => a.Libelle == annee_lib).First().Id;
+        }
+    }
+
+    /// <summary>
+    /// Classe contenant les DataAnnotations pour chaque champ
+    /// </summary>
+    public class Annee_Metadata
+    {
+    }
+}

+ 3 - 15
CD67.FicheCollege.MVC/Controllers/HomeController.cs

@@ -1,29 +1,17 @@
 using CD67.FicheCollege.Entity;
 using System;
 using System.Linq;
+using System.Net;
 using System.Web.Mvc;
 
 namespace CD67.FicheCollege.MVC.Controllers
 {
     public class HomeController : Controller
     {
-        private Entities db = new Entities();
-
         // GET: Home
         public ActionResult Index()
         {
-            string annee_lib;
-
-            if (DateTime.Now.Month <= 6) {
-                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year - 1, DateTime.Now.Year);
-            }
-            else {
-                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year, DateTime.Now.Year + 1);
-            }
-
-            int annee_id = db.Annees.Where(a => a.Libelle == annee_lib).First().Id;
-
-            return RedirectToActionPermanent("Details", "Annees", new { id = annee_id });
+            return RedirectToAction("Details", "Annees", new { id = Annee.get_current_year_id() });
         }
     }
-}
+}