Bladeren bron

NEW Ecran de gestion des collèges avec un ViewModel

julien.legrand 8 jaren geleden
bovenliggende
commit
d0a77955ff

+ 6 - 0
CD67.FicheCollege.MVC/CD67.FicheCollege.MVC.csproj

@@ -157,6 +157,7 @@
   <ItemGroup>
     <Compile Include="App_Start\BundleConfig.cs" />
     <Compile Include="Controllers\AdminController.cs" />
+    <Compile Include="Controllers\CollegesController.cs" />
     <Compile Include="Controllers\HomeController.cs" />
     <Compile Include="Controllers\TerritoireController.cs" />
     <Compile Include="Controllers\TypeCollegeController.cs" />
@@ -167,6 +168,8 @@
     <Compile Include="Internal\MvcHtmlHelpers.cs" />
     <Compile Include="Internal\Navigation.cs" />
     <Compile Include="Internal\UtilisateurConnecteFactory.cs" />
+    <Compile Include="Models\CollegeViewModel.cs" />
+    <Compile Include="Models\ModeAcces.cs" />
     <Compile Include="Models\UtilisateurConnecte.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
@@ -367,6 +370,9 @@
     <Content Include="Views\Territoire\Delete.cshtml" />
     <Content Include="Views\Territoire\Edit.cshtml" />
     <Content Include="Views\Territoire\Index.cshtml" />
+    <Content Include="Views\Colleges\Details.cshtml" />
+    <Content Include="Views\Colleges\Edit.cshtml" />
+    <Content Include="Views\Colleges\Index.cshtml" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="App_Data\" />

+ 155 - 0
CD67.FicheCollege.MVC/Controllers/CollegesController.cs

@@ -0,0 +1,155 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Entity;
+using System.Linq;
+using System.Net;
+using System.Web;
+using System.Web.Mvc;
+using CD67.FicheCollege.Entity;
+using CD67.FicheCollege.Factory;
+
+namespace CD67.FicheCollege.MVC.Controllers
+{
+    public class CollegesController : Controller
+    {
+        private Entities db = new Entities();
+
+        // GET: College
+        public ActionResult Index()
+        {
+            CollegeFactory collegeFactory = new CollegeFactory(db);
+            return View(collegeFactory.getAll());
+        }
+
+        // GET: College/Details/5
+        public ActionResult Details(string id)
+        {
+            if (id == null)
+            {
+                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
+            }
+            CollegeFactory collegeFactory = new CollegeFactory(db);
+            Entity.College college = collegeFactory.getById(id);
+            if (college == null)
+            {
+                return HttpNotFound();
+            }
+            return View(GetViewModel(college, Models.ModeAcces.Lecture));
+        }
+
+        // GET: College/Create
+        public ActionResult Create()
+        {
+            Entity.College college = new Entity.College();
+            return View("Edit", GetViewModel(college, Models.ModeAcces.Creation));
+        }
+
+        // POST: College/Create
+        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
+        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Create(College college)
+        {
+            if (ModelState.IsValid)
+            {
+                CollegeFactory collegeFactory = new CollegeFactory(db);
+                collegeFactory.add(ref college);
+                return RedirectToAction("Index");
+            }
+
+            return View("Edit", GetViewModel(college, Models.ModeAcces.Creation));
+        }
+
+        // GET: College/Edit/5
+        public ActionResult Edit(string id)
+        {
+            if (id == null)
+            {
+                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
+            }
+            CollegeFactory collegeFactory = new CollegeFactory(db);
+            Entity.College college = collegeFactory.getById(id);
+            if (college == null)
+            {
+                return HttpNotFound();
+            }
+            return View(GetViewModel(college, Models.ModeAcces.Modification));
+        }
+
+        // POST: College/Edit/5
+        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
+        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
+        [HttpPost]
+        [ValidateAntiForgeryToken]
+        public ActionResult Edit(College college)
+        {
+            if (ModelState.IsValid)
+            {
+                CollegeFactory collegeFactory = new CollegeFactory(db);
+                collegeFactory.update(ref college);
+                return RedirectToAction("Details", new { Id = college.Id });
+            }
+            return View(GetViewModel(college, Models.ModeAcces.Modification));
+        }
+
+        // GET: College/Delete/5
+        public ActionResult Delete(string id)
+        {
+            if (id == null)
+            {
+                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
+            }
+            CollegeFactory collegeFactory = new CollegeFactory(db);
+            Entity.College college = collegeFactory.getById(id);
+            if (college == null)
+            {
+                return HttpNotFound();
+            }
+            return View("Details", GetViewModel(college, Models.ModeAcces.Suppression));
+        }
+
+        // POST: College/Delete/5
+        [HttpPost, ActionName("Delete")]
+        [ValidateAntiForgeryToken]
+        public ActionResult DeleteConfirmed(string id)
+        {
+            CollegeFactory collegeFactory = new CollegeFactory(db);
+            Entity.College college = collegeFactory.getById(id);
+            collegeFactory.delete(ref college);
+            return RedirectToAction("Index");
+        }
+
+        private Models.CollegeViewModel GetViewModel(Entity.College college, Models.ModeAcces Acces)
+        {
+            Dictionary<string, SelectList> listes = new Dictionary<string, SelectList>();
+
+            //On prépare les listes de choix en création ou modification
+            if (Acces == Models.ModeAcces.Creation || Acces == Models.ModeAcces.Modification)
+            {
+                TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
+                SelectList listeTypeCollege = new SelectList(typeCollegeFactory.getAll("Ordre"), "Id", "Libelle", college.TypeCollege_Id);
+                listes.Add("TypeCollege_Id", listeTypeCollege);
+            }
+
+            return new Models.CollegeViewModel()
+            {
+                College_Id = college.Id,
+                College_Libelle = college.Libelle,
+                Contenu = college,
+                Acces = Acces,
+                Listes = listes
+            };
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                db.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+    }
+}

+ 40 - 0
CD67.FicheCollege.MVC/Internal/MvcHtmlHelpers.cs

@@ -1,5 +1,8 @@
 using System;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
 using System.Linq.Expressions;
+using System.Web;
 using System.Web.Mvc;
 
 namespace CD67.FicheCollege.MVC
@@ -23,5 +26,42 @@ namespace CD67.FicheCollege.MVC
 
             return MvcHtmlString.Create(string.Format(@"<span>{0}</span>", description));
         }
+
+        /// <summary>
+        /// Affiche le "display name" pour un élément d'énumération
+        /// </summary>
+        /// <param name="item">Elément d'énumaration courant</param>
+        /// <param name="value">Valeur souhaitée</param>
+        /// <returns>Valeur de l'annotation Display name de l'élément d'énumération courant</returns>
+        public static HtmlString EnumDisplayNameFor(this Enum item, DisplayValue value)
+        {
+            var type = item.GetType();
+            var member = type.GetMember(item.ToString());
+            DisplayAttribute displayAttribute = (DisplayAttribute)member[0].GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
+
+            if (displayAttribute != null)
+            {
+                switch (value)
+                {
+                    case DisplayValue.Name:
+                        return new HtmlString(displayAttribute.Name);
+                    case DisplayValue.Prompt:
+                        return new HtmlString(displayAttribute.Prompt);
+                    case DisplayValue.Description:
+                        return new HtmlString(displayAttribute.Description);
+                    default:
+                        return new HtmlString(item.ToString());
+                }
+            }
+
+            return new HtmlString(item.ToString());
+        }
+
+        public enum DisplayValue
+        {
+            Name,
+            Prompt,
+            Description
+        }
     }
 }

+ 17 - 0
CD67.FicheCollege.MVC/Models/CollegeViewModel.cs

@@ -0,0 +1,17 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+
+namespace CD67.FicheCollege.MVC.Models
+{
+    public class CollegeViewModel
+    {
+        public string College_Id { get; set; }
+        public string College_Libelle { get; set; }
+        public object Contenu { get; set; }
+        public ModeAcces Acces { get; set; }
+        public Dictionary<String, SelectList> Listes { get; set; }
+    }
+}

+ 20 - 0
CD67.FicheCollege.MVC/Models/ModeAcces.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Web;
+
+namespace CD67.FicheCollege.MVC.Models
+{
+    public enum ModeAcces
+    {
+        [Display(Prompt = "Ajouter")]
+        Creation,
+        [Display(Prompt = "Lire")]
+        Lecture,
+        [Display(Prompt = "Modifier")]
+        Modification,
+        [Display(Prompt = "Supprimer")]
+        Suppression
+    }
+}

+ 111 - 0
CD67.FicheCollege.MVC/Views/Colleges/Details.cshtml

@@ -0,0 +1,111 @@
+@using CD67.FicheCollege.MVC.Models
+@model CollegeViewModel
+
+@{
+    ViewBag.Title = "Details";
+    Layout = "~/Views/Shared/_Layout.cshtml";
+    College college = Model.Contenu as College;
+}
+
+<h1>@college.Libelle</h1>
+@if(Model.Acces==ModeAcces.Suppression) {
+    <h2 class="text-danger">Voulez-vous vraiment supprimer cet élément?</h2>
+    <p class="text-danger">
+        Le collège ainsi que toutes les informations liées seront supprimées.
+    </p>
+}
+
+<fieldset>
+    <legend>
+        Détails du collège
+        @if (Model.Acces == ModeAcces.Lecture)
+        {
+            <div class="pull-right">
+                @Html.ActionLink("Supprimer", "Delete", "Colleges", new { Id = college.Id }, new { @class = "btn btn-danger" })
+                @Html.ActionLink("Modifier", "Edit", "Colleges", new { Id = college.Id }, new { @class = "btn btn-default" })
+            </div>
+        }
+    </legend>
+    <dl class="dl-horizontal">
+        <dt>
+            @Html.DisplayNameFor(model => college.TypeCollege.Libelle)
+        </dt>
+
+        <dd>
+            @Html.DisplayFor(model => college.TypeCollege.Libelle)
+        </dd>
+
+        <dt>
+            @Html.DisplayNameFor(model => college.Adresse)
+        </dt>
+
+        <dd>
+            @Html.DisplayFor(model => college.AdresseComplete)
+        </dd>
+
+        <dt>
+            @Html.DisplayNameFor(model => college.Tel)
+        </dt>
+
+        <dd>
+            @Html.DisplayFor(model => college.Tel)
+        </dd>
+
+        <dt>
+            @Html.DisplayNameFor(model => college.Fax)
+        </dt>
+
+        <dd>
+            @Html.DisplayFor(model => college.Fax)
+        </dd>
+
+        <dt>
+            @Html.DisplayNameFor(model => college.Email)
+        </dt>
+
+        <dd>
+            @Html.DisplayFor(model => college.Email)
+        </dd>
+
+        <dt>
+            @Html.DisplayNameFor(model => college.Commune)
+        </dt>
+
+        <dd>
+            @if (college.Commune != null)
+        {
+                <div class="col-sm-8 col-md-4">
+                    <div class="panel panel-default">
+                        <div class="panel-heading clearfix">
+                            <h4 class="panel-title pull-left" style="padding-top: 7.5px;">
+                                <i class="fa fa-map-marker color1" aria-hidden="true"></i>
+                                Localisation (commune)
+                            </h4>
+                        </div>
+                        <div class="panel-body" style="text-align: center;">
+                            <span style="white-space: pre-line;">
+                                <b>@college.Commune</b>
+                                Canton : @college.Canton
+                                TAD : @college.TAD
+                                CDC : @college.CDC
+                            </span>
+                        </div>
+                    </div>
+                </div>
+            }
+        </dd>
+    </dl>
+</fieldset>
+
+@if (Model.Acces == ModeAcces.Suppression)
+{
+    using (Html.BeginForm())
+    {
+        @Html.AntiForgeryToken()
+
+        <div class="form-actions no-color">
+            <input type="submit" value="@Model.Acces.EnumDisplayNameFor(MvcHtmlHelpers.DisplayValue.Prompt)" class="btn btn-danger" /> |
+            @Html.ActionLink("Annuler", "Details", new { Id = Model.College_Id })
+        </div>
+    }
+}

+ 157 - 0
CD67.FicheCollege.MVC/Views/Colleges/Edit.cshtml

@@ -0,0 +1,157 @@
+@using CD67.FicheCollege.MVC.Models
+@model CollegeViewModel
+
+@{
+    ViewBag.Title = "Ajout";
+    Layout = "~/Views/Shared/_Layout.cshtml";
+    College college = Model.Contenu as College;
+}
+
+<h2>@Model.Acces.ToString()</h2>
+
+
+@using (Html.BeginForm())
+{
+    @Html.AntiForgeryToken()
+
+    <div class="form-horizontal">
+        <h4>Collège</h4>
+        <hr />
+        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
+
+        @if (Model.Acces == ModeAcces.Modification)
+        {
+            @Html.HiddenFor(model => college.Id)
+        }
+        <div class="form-group">
+            @Html.LabelFor(model => college.Id, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @if (Model.Acces == ModeAcces.Modification)
+                {
+                    @Html.EditorFor(model => college.Id, new { htmlAttributes = new { @class = "form-control", @disabled = true } })
+                }
+                else
+                {
+                    @Html.EditorFor(model => college.Id, new { htmlAttributes = new { @class = "form-control" } })
+                }
+                @Html.ValidationMessageFor(model => college.Id, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.Libelle, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.EditorFor(model => college.Libelle, new { htmlAttributes = new { @class = "form-control" } })
+                @Html.ValidationMessageFor(model => college.Libelle, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.TypeCollege_Id, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.DropDownListFor(model => college.TypeCollege_Id, Model.Listes["TypeCollege_Id"], htmlAttributes: new { @class = "form-control" })
+                @Html.ValidationMessageFor(model => college.TypeCollege_Id, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.Adresse, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.EditorFor(model => college.Adresse, new { htmlAttributes = new { @class = "form-control" } })
+                @Html.ValidationMessageFor(model => college.Adresse, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        @Html.HiddenFor(model => college.Code_Postal, new { data_picker = "commune.code_postal" })
+        @Html.HiddenFor(model => college.Commune_Insee, new { data_picker = "commune.insee" })
+        @Html.HiddenFor(model => college.Commune, new { data_picker = "commune.nom" })
+        @Html.HiddenFor(model => college.Canton, new { data_picker = "commune.canton" })
+        @Html.HiddenFor(model => college.Territoire_Id, new { data_picker = "commune.code_tad" })
+        @Html.HiddenFor(model => college.TAD, new { data_picker = "commune.tad" })
+        @Html.HiddenFor(model => college.CDC, new { data_picker = "commune.cdc" })
+        <div class="form-group">
+            @Html.LabelFor(model => college.Commune, htmlAttributes: new { @class = "control-label col-md-2 required" })
+            <div class="col-md-4">
+                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/com67/" title="Selection commune (nouvelle fenetre)">Sélectionner une commune</a>
+                <br />
+                <div class="panel panel-default" id="Commune_Panel" name="Commune_Panel" style="@if (college.Commune_Insee == null) { <text>display: none;</text> }">
+                    <div class="panel-heading clearfix">
+                        <h4 class="panel-title pull-left" style="padding-top: 7.5px;">
+                            <i class="fa fa-map-marker color1" aria-hidden="true"></i>
+                            Localisation (commune)
+                        </h4>
+                        <div class="pull-right">
+                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="commune"></span>
+                        </div>
+                    </div>
+                    <div class="panel-body" style="text-align: center;">
+                        <span>
+                            <b><span data-picker="commune.nom">@college.Commune</span ></b >
+                            <br />Canton : <span data-picker="commune.canton">@college.Canton</span >
+                            <br />TAD : <span data-picker="commune.tad">@college.TAD</span >
+                            <br />CDC : <span data-picker="commune.cdc">@college.CDC</span >
+                        </span>
+                    </div>
+                </div>
+                @Html.ValidationMessageFor(model => college.Commune, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.Tel, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.EditorFor(model => college.Tel, new { htmlAttributes = new { @class = "form-control" } })
+                @Html.ValidationMessageFor(model => college.Tel, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.Fax, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.EditorFor(model => college.Fax, new { htmlAttributes = new { @class = "form-control" } })
+                @Html.ValidationMessageFor(model => college.Fax, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            @Html.LabelFor(model => college.Email, htmlAttributes: new { @class = "control-label col-md-2" })
+            <div class="col-md-10">
+                @Html.EditorFor(model => college.Email, new { htmlAttributes = new { @class = "form-control" } })
+                @Html.ValidationMessageFor(model => college.Email, "", new { @class = "text-danger" })
+            </div>
+        </div>
+
+        <div class="form-group">
+            <div class="col-md-offset-2 col-md-10">
+                <input type="submit" value="@Model.Acces.EnumDisplayNameFor(MvcHtmlHelpers.DisplayValue.Prompt)" class="btn btn-default" /> |
+                @if (Model.Acces == ModeAcces.Creation)
+                { @Html.ActionLink("Annuler", "Index") }
+                else
+                { @Html.ActionLink("Annuler", "Details", new { Id = Model.College_Id }) }
+            </div>
+        </div>
+    </div>
+}
+
+@section Scripts {
+    <script type="text/javascript">
+            $(".removeItem").click(function () {
+                switch ($(this).data("type")) {
+                    case 'commune':
+                        $('#Code_Postal').val(null);
+                        $('#Commune_Insee').val(null);
+                        $('#Commune').val(null);
+                        $('#Canton').val(null);
+                        $('#Territoire_Id').val(null);
+                        $('#TAD').val(null);
+                        $('#CDC').val(null);
+                        $('#Commune_Panel').hide();
+                        break;
+                    default:
+                        return false;
+                }
+            });
+    </script>
+
+    @Scripts.Render("~/bundles/jqueryval")
+}

+ 74 - 0
CD67.FicheCollege.MVC/Views/Colleges/Index.cshtml

@@ -0,0 +1,74 @@
+@model IEnumerable<CD67.FicheCollege.Entity.College>
+
+@{
+    ViewBag.Title = "Liste";
+    Layout = "~/Views/Shared/_Layout.cshtml";
+}
+
+<h2>Index</h2>
+
+<p>
+    <a href="@Url.Action("Create")">
+        <span class="glyphicon glyphicon-plus-sign fa-2x color1" style="vertical-align: middle" aria-hidden="true"></span>
+        Ajouter un nouveau collège
+    </a>
+</p>
+
+<table class="table">
+    <tr>
+        <th>
+            @Html.DisplayNameFor(model => model.Id)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.Adresse)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.CDC)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.Tel)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.Fax)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.Email)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.Territoire.Libelle)
+        </th>
+        <th>
+            @Html.DisplayNameFor(model => model.TypeCollege.Libelle)
+        </th>
+    </tr>
+
+@foreach (var item in Model) {
+    <tr>
+        <th>
+            @Html.ActionLink(item.Id, "Details", new { id = item.Id })
+        </th>
+        <td>
+            @Html.DisplayFor(modelItem => item.AdresseComplete)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.CDC)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Tel)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Fax)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Email)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Territoire.Libelle)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.TypeCollege.Libelle)
+        </td>
+    </tr>
+}
+
+</table>

+ 3 - 6
CD67.FicheCollege.MVC/Views/Home/Index.cshtml

@@ -5,10 +5,7 @@
     Layout = "~/Views/Shared/_Layout.cshtml";
 }
 
-<h1>Accueil</h1>
+<h2>Accueil</h2>
 
-<div>
-    <ul id="menu-l2">
-        <li id="li-menu-l2"><a id="a-li-menu-l2-active" class="btn-sm" href="~/">Sous&#160;menu&#160;1</a></li>
-    </ul>
-</div>
+@Html.ActionLink("Les collèges", "Index", "Colleges")<br />
+@Html.ActionLink("La page d'administration du site", "Index", "Admin")<br />

+ 1 - 0
CD67.FicheCollege.MVC/Views/Web.config

@@ -18,6 +18,7 @@
         <add namespace="System.Web.Routing" />
         <add namespace="System.Web.Optimization" />
         <add namespace="CD67.FicheCollege.MVC" />
+        <add namespace="CD67.FicheCollege.Entity" />
       </namespaces>
     </pages>
   </system.web.webPages.razor>