{"id":1067,"date":"2012-09-09T03:19:00","date_gmt":"2012-09-09T03:19:00","guid":{"rendered":"http:\/\/nestor.nestasoft.fr\/?p=1067"},"modified":"2012-10-19T04:51:42","modified_gmt":"2012-10-19T04:51:42","slug":"asp-net-mvc-4-seconde-partie-ajout-dun-contrleur","status":"publish","type":"post","link":"https:\/\/nestasoft.fr\/?p=1067","title":{"rendered":"ASP.NET MVC 4 [Troisi\u00e8me partie] : Ajout d&rsquo;un contr\u00f4leur"},"content":{"rendered":"<p><a href=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/ApresCreatyionAdherent.PNG\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Contr\u00f4leur\" border=\"0\" alt=\"ASP NET MVC4\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/ApresCreatyionAdherent.PNG\" width=\"600\" height=\"225\" \/><\/a><\/p>\n<ol>\n<li><strong><font color=\"#4f81bd\"><a href=\"http:\/\/nestor.nestasoft.fr\/?p=1012\" target=\"_blank\">ASP.NET MVC 4 [Premi\u00e8re partie]&#160;&#160; : Introduction<\/a><\/font><\/strong> <\/li>\n<li><strong><font color=\"#4f81bd\"><a href=\"http:\/\/nestor.nestasoft.fr\/?p=1059\" target=\"_blank\">ASP.NET MVC 4 [Deuxi\u00e8me partie]&#160; : Ajout d\u2019un model<\/a><\/font> <\/strong><\/li>\n<li><font color=\"#4f81bd\"><strong>ASP.NET MVC 4 [Troisi\u00e8me partie]&#160; : Ajout d\u2019un contr\u00f4leur<\/strong><\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Quatri\u00e8me partie] : Ajout d\u2019une vue<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Cinqui\u00e8me partie] : Page Inspector<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Sixi\u00e8me partie]&#160;&#160;&#160;&#160;&#160; : Razor ou ASPX<\/font> <\/li>\n<li><font color=\"#4f81bd\">ASP.NET MVC 4 [Septi\u00e8me partie]&#160;&#160; : Conclusion<\/font> <\/li>\n<\/ol>\n<p>Petit rappel, Le <b>contr\u00f4leur<\/b> : interpr\u00e8te les actions \u00e0 r\u00e9aliser et ordonne l\u2019ex\u00e9cution de celles-ci; c\u2019est le pilote de l\u2019application.<\/p>\n<p>Dans cette partie, nous ajouterons un contr\u00f4leur \u00e0 notre application.<\/p>\n<p><!--more--><\/p>\n<p><a href=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/Controleur.PNG\"><img decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/Controleur.PNG\" \/><\/a><\/p>\n<p><strong>Remarque<\/strong> : Si vous choisissez ASP.NET(C#) au lieu de Razor(CSHTML), Visual Studio 2012 g\u00e9n\u00e9rera des fichiers .aspx au lieu du .cshtml.<\/p>\n<p>\u00c9tant donn\u00e9 que notre model a \u00e9t\u00e9 mis en place, VS2012 nous rend la t\u00e2che facile. En effet, notre classe CSControleur est g\u00e9n\u00e9r\u00e9e et contient la quasi-totalit\u00e9 des m\u00e9thodes qui nous seront n\u00e9cessaires (Index, Detail, Create, Edit, Delet et deleteConfirmed). Je d\u00e9taillerai quelques unes de ces m\u00e9thodes par la suite.<\/p>\n<p>Nous remarquons l\u2019ajout de dossier \u201cCS\u201d contenant 5 fichiers dans notre solution:<\/p>\n<p><a href=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/FichierGenereAvecRazor.PNG\"><img decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/nestor.nestasoft.fr\/wp-content\/uploads\/2012\/09\/FichierGenereAvecRazor.PNG\" \/><\/a><\/p>\n<p>Visual Studio 2012 cr\u00e9e :<\/p>\n<li>6 m\u00e9thodes&#160; dans le fichier CSControleur <\/li>\n<li>Un dossier \u201cCS\u201d <\/li>\n<li>Le fichier Create.cshtml <\/li>\n<li>Le fichier Delete.cshtml <\/li>\n<li>Le fichier Detail.cshtml <\/li>\n<li>Le fichier Edit.cshtml <\/li>\n<li>Le fichier Index.cshtml\n<p>Examinons quelques M\u00e9thode du fichier CSControleur: <\/p>\n<p>&#160;<\/p>\n<p>M\u00e9thode <font color=\"#008000\"><strong>Index()<\/strong>         <br \/><\/font><\/p>\n<pre class=\"brush:csharp\"><p>private AdherentDbContext db = new AdherentDbContext();<\/p>\r\n<p>public ActionResult Index()\r\n{&#160;&#160;&#160;&#160;&#160; return View(db.Adherents.ToList());\r\n}<\/p><\/pre>\n<p>Notre contr\u00f4leur retournera la liste des adh\u00e9rents de la base de donn\u00e9e et transmettra le r\u00e9sultat \u00e0 l\u2019index de la vue en question.<\/p>\n<p>&#160;<\/p>\n<p>M\u00e9thode <font color=\"#008000\"><strong>Details()<\/strong><\/font><\/p>\n<pre class=\"brush:csharp\"> public ActionResult Details(int id = 0)\r\n {\r\n      Adherent adherent = db.Adherents.Find(id);\r\n      if (adherent == null)\r\n      {\r\n           return HttpNotFound();\r\n      }\r\n      return View(adherent);\r\n }<\/pre>\n<p>Cette m\u00e9thode, retourne une instance du model Adherent \u00e0 la vue Detail, si un adh\u00e9rent est trouv\u00e9.<\/p>\n<p>&#160;<\/p>\n<p>M\u00e9thode <font color=\"#008000\"><strong>Create()<\/strong><\/font><\/p>\n<pre class=\"brush:csharp\">public ActionResult Create(Adherent adherent)\r\n{\r\n   if (ModelState.IsValid)\r\n   {\r\n        db.Adherents.Add(adherent);\r\n        db.SaveChanges();\r\n        return RedirectToAction(&quot;Index&quot;);\r\n   }\r\n\r\n   return View(adherent);\r\n}<\/pre>\n<p>La m\u00e9thode Create() v\u00e9rifie si le mod\u00e8le est valide; si oui, ajoute un adh\u00e9rent, effectue la sauvegarde redirige la page sur l\u2019index de l\u2019adh\u00e9rent cr\u00e9e.<\/p>\n<p>La ligne<font style=\"background-color: #ffff00\"> <strong>@model CSMvc4.Models.Adherent<\/strong> :<\/font><\/p>\n<p>Cette ligne se trouve au d\u00e9but des 5 fichiers g\u00e9n\u00e9r\u00e9s par Visual Studio 2012. Cette directive permet d\u2019acc\u00e9der \u00e0 la liste de tous les adh\u00e9rents que le contr\u00f4leur envoie \u00e0 la vue en question.<\/p>\n<p>Dans le post suivant nous parlerons des vues.<\/p>\n<p>Be connected!<\/p>\n<\/li>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1067\" class=\"pvc_stats all  \" data-element-id=\"1067\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET MVC 4 [Premi\u00e8re partie]&#160;&#160; : Introduction ASP.NET MVC 4 [Deuxi\u00e8me partie]&#160; : Ajout d\u2019un model ASP.NET MVC 4 [Troisi\u00e8me partie]&#160; : Ajout d\u2019un contr\u00f4leur ASP.NET MVC 4 [Quatri\u00e8me partie] : Ajout d\u2019une vue ASP.NET MVC 4 [Cinqui\u00e8me partie] : Page Inspector ASP.NET MVC 4 [Sixi\u00e8me partie]&#160;&#160;&#160;&#160;&#160; : Razor ou ASPX ASP.NET MVC 4 [Septi\u00e8me [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1067\" class=\"pvc_stats all  \" data-element-id=\"1067\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/nestasoft.fr\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":1162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1067","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1067"}],"version-history":[{"count":14,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1067\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/posts\/1067\/revisions\/1121"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=\/wp\/v2\/media\/1162"}],"wp:attachment":[{"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nestasoft.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}