Przeglądaj źródła

NEW Ajout des messages flash

julien.legrand 8 lat temu
rodzic
commit
6524cf736c

+ 2 - 0
CD67.ModeleMVC.MVC/CD67.ModeleMVC.MVC.csproj

@@ -159,6 +159,7 @@
     <Compile Include="Global.asax.cs">
       <DependentUpon>Global.asax</DependentUpon>
     </Compile>
+    <Compile Include="Internal\FlashMessageExtensions.cs" />
     <Compile Include="Internal\MvcHtmlHelpers.cs" />
     <Compile Include="Internal\Navigation.cs" />
     <Compile Include="Internal\UtilisateurConnecteFactory.cs" />
@@ -318,6 +319,7 @@
     <Content Include="Views\Shared\DisplayTemplates\Date.cshtml" />
     <Content Include="Views\Shared\EditorTemplates\Date.cshtml" />
     <Content Include="Views\PrintTest\Index.cshtml" />
+    <Content Include="Views\Shared\_Flash.cshtml" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="App_Data\" />

+ 13 - 0
CD67.ModeleMVC.MVC/Controllers/TYPE_VIKINGController.cs

@@ -2,6 +2,7 @@
 using System.Web.Mvc;
 using CD67.ModeleMVC.Entity;
 using CD67.ModeleMVC.Factory;
+using CD67.ModeleMVC.MVC.Internal;
 
 namespace CD67.ModeleMVC.MVC.Controllers
 {
@@ -50,6 +51,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             {
                 TYPE_VIKINGFactory typeVikingFactory = new TYPE_VIKINGFactory(db);
                 typeVikingFactory.add(ref typeViking);
+
+                // Ajout d'un message flash
+                this.Success("Type de viking créé avec succès.");
+
                 return RedirectToAction("Index");
             }
 
@@ -83,6 +88,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             {
                 TYPE_VIKINGFactory typeVikingFactory = new TYPE_VIKINGFactory(db);
                 typeVikingFactory.update(ref typeViking);
+
+                // Ajout d'un message flash
+                this.Success("Type de viking edité avec succès.");
+
                 return RedirectToAction("Index");
             }
             return View(typeViking);
@@ -112,6 +121,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             TYPE_VIKINGFactory typeVikingFactory = new TYPE_VIKINGFactory(db);
             EXEMPLE_TYPE_VIKING typeViking = typeVikingFactory.getById(id);
             typeVikingFactory.delete(ref typeViking);
+
+            // Ajout d'un message flash
+            this.Success("Type de viking supprimé avec succès.");
+
             return RedirectToAction("Index");
         }
 

+ 13 - 0
CD67.ModeleMVC.MVC/Controllers/VIKINGSController.cs

@@ -5,6 +5,7 @@ using CD67.ModeleMVC.Factory;
 using System.Collections.Generic;
 using System;
 using System.Linq;
+using CD67.ModeleMVC.MVC.Internal;
 
 namespace CD67.ModeleMVC.MVC.Controllers
 {
@@ -60,6 +61,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             {
                 VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
                 vikingsFactory.add(ref viking);
+
+                // Ajout d'un message flash
+                this.Success("Viking créé avec succès.");
+
                 return RedirectToAction("Index");
             }
             FillSelect(viking);
@@ -94,6 +99,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             {
                 VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
                 vikingsFactory.update(ref viking);
+
+                // Ajout d'un message flash
+                this.Success("Viking édité avec succès.");
+
                 return RedirectToAction("Index");
             }
             FillSelect(viking);
@@ -124,6 +133,10 @@ namespace CD67.ModeleMVC.MVC.Controllers
             VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
             EXEMPLE_VIKINGS viking = vikingsFactory.getById(id);
             vikingsFactory.delete(ref viking);
+
+            // Ajout d'un message flash
+            this.Success("Viking supprimé avec succès.");
+
             return RedirectToAction("Index");
         }
 

+ 45 - 0
CD67.ModeleMVC.MVC/Internal/FlashMessageExtensions.cs

@@ -0,0 +1,45 @@
+using System.Web;
+using System.Web.Mvc;
+
+namespace CD67.ModeleMVC.MVC.Internal
+{
+    internal static class FlashMessageExtensions
+    {
+        public static Controller Error(this Controller result, string message)
+        {
+            CreateFlashMessage(Notification.Error, message);
+            return result;
+        }
+
+        public static Controller Warning(this Controller result, string message)
+        {
+            CreateFlashMessage(Notification.Warning, message);
+            return result;
+        }
+
+        public static Controller Success(this Controller result, string message)
+        {
+            CreateFlashMessage(Notification.Success, message);
+            return result;
+        }
+
+        public static Controller Information(this Controller result, string message)
+        {
+            CreateFlashMessage(Notification.Info, message);
+            return result;
+        }
+
+        private static void CreateFlashMessage(Notification notification, string message)
+        {
+            System.Web.HttpContext.Current.Session[notification.ToString()] = message;
+        }
+
+        private enum Notification
+        {
+            Error,
+            Warning,
+            Success,
+            Info
+        }
+    }
+}

+ 44 - 0
CD67.ModeleMVC.MVC/Views/Shared/_Flash.cshtml

@@ -0,0 +1,44 @@
+
+<div style="padding-top: 25px;">
+
+@if (Session["Error"] != null && !string.IsNullOrWhiteSpace(Session["Error"].ToString()))
+{
+    <br />
+    <div id="flash-messages" class=" alert alert-dismissible alert-danger">    
+        <a class="close" data-dismiss="alert" href="#">&times;</a>
+        <p>@Session["Error"].ToString()</p>
+    </div> 
+    Session.Remove("Error");
+}
+
+@if (Session["Success"] != null && !string.IsNullOrWhiteSpace(Session["Success"].ToString()))
+{
+    <br />
+    <div id="flash-messages" class="alert  alert-dismissible alert-success">    
+        <a class="close" data-dismiss="alert" href="#">&times;</a>
+        <p>@Session["Success"].ToString()</p>
+    </div> 
+    Session.Remove("Success");
+}
+
+@if (Session["Warning"] != null && !string.IsNullOrWhiteSpace(Session["Warning"].ToString()))
+{
+    <br />
+    <div id="flash-messages" class="alert alert-dismissible alert-warning">    
+        <a class="close" data-dismiss="alert" href="#">&times;</a>
+        <p>@Session["Warning"].ToString()</p>
+    </div> 
+    Session.Remove("Warning");
+}
+
+@if (Session["Info"] != null && !string.IsNullOrWhiteSpace(Session["Info"].ToString()))
+{
+    <br />
+    <div id="flash-messages" class="alert alert-dismissible alert-info">    
+        <a class="close" data-dismiss="alert" href="#">&times;</a>
+        <p>@Session["Info"].ToString()</p>
+    </div>
+
+    Session.Remove("Info");
+}
+</div>

+ 1 - 0
CD67.ModeleMVC.MVC/Views/Shared/_Layout.cshtml

@@ -105,6 +105,7 @@
             @Html.MvcSiteMap().SiteMapPath()
         </div>
         <div id="content" >
+            @Html.Partial("_Flash")
             @RenderBody()
         </div>
     </div>