﻿
{"id":1036,"date":"2012-11-26T14:42:07","date_gmt":"2012-11-26T14:42:07","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=1036"},"modified":"2012-11-28T14:14:35","modified_gmt":"2012-11-28T14:14:35","slug":"mvcde-detailscreateeditdelete-metodlari-1","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/mvcde-detailscreateeditdelete-metodlari-1\/","title":{"rendered":"MVC&#8217;de Details,Create,Edit,Delete Metodlar\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Bu yaz\u0131 dizimizde MVC projemizde Controller olu\u015ftururken &#8220;Add action methods for Create, Update, Delete and Details scenarios&#8221; ifadesinden bahsedelim.<\/p>\n<p>Bu yaz\u0131 dizimizde, bu g\u00fcne kadar bahsetti\u011fim MVC konular\u0131m\u0131z\u0131 kapsayan, ayr\u0131nt\u0131l\u0131 ve teferruatl\u0131 bir \u00f6rnek \u015femas\u0131 \u00e7izece\u011fiz.Art\u0131k konular\u0131m\u0131z derinle\u015ftik\u00e7e alg\u0131lamam\u0131z ve di\u011fer yap\u0131larla b\u00fct\u00fcnl\u00fck kurabilmemiz i\u00e7in derin ve kompleks \u00f6rneklerle yaz\u0131lar\u0131m\u0131 i\u015flemekten \u00e7ekinmeyece\u011fim.<br \/>\n<!--more--><\/p>\n<p>As\u0131l konumuza d\u00f6nersek e\u011fer, bu yaz\u0131dan itibaren bir yol belirtip, yaz\u0131 dizimiz boyunca bu yol \u00fczerinden devam edece\u011fim.<\/p>\n<p>\u00d6ncelikle Asp.NET MVC konular\u0131m\u0131zda veritaban\u0131 ile i\u015flem yapmay\u0131 g\u00f6rmedi\u011fimizden dolay\u0131 bu yaz\u0131 dizimde Model katman\u0131nda olu\u015fturdu\u011fumuz bir Entity ile, o Entity s\u0131n\u0131f\u0131n\u0131n tipinden olan bir List koleksiyonu \u00fczerinden \u00e7al\u0131\u015faca\u011f\u0131m.<\/p>\n<p>Evet art\u0131k konumuzu incelemeye ba\u015flayabiliriz.Bir Asp.NET MVC projesi olu\u015fturup, &#8220;Ogrenci(Controller)&#8221; ad\u0131nda bir Controller s\u0131n\u0131f\u0131 olu\u015ftural\u0131m.Eee haliyle bu yaz\u0131n\u0131n ana konusu olan &#8220;<strong><em><span style=\"font-size: x-small;\">Add action methods for Create, Update, Delete and Details scenarios<\/span><\/em><\/strong>&#8221; ifadesini i\u015faretlemeyi unutmayal\u0131m.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Controller1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1037\" title=\"Controller\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Controller1.png\" alt=\"\" width=\"493\" height=\"201\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Controller1.png 493w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Controller1-300x122.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/a><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class OgrenciController : Controller\r\n    {\r\n        \/\/\r\n        \/\/ GET: \/Ogrenci\/\r\n\r\n        public ActionResult Index()\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/\r\n        \/\/ GET: \/Ogrenci\/Details\/5\r\n\r\n        public ActionResult Details(int id)\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/\r\n        \/\/ GET: \/Ogrenci\/Create\r\n\r\n        public ActionResult Create()\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/\r\n        \/\/ POST: \/Ogrenci\/Create\r\n\r\n        &#x5B;HttpPost]\r\n        public ActionResult Create(FormCollection collection)\r\n        {\r\n            try\r\n            {\r\n                \/\/ TODO: Add insert logic here\r\n\r\n                return RedirectToAction(&quot;Index&quot;);\r\n            }\r\n            catch\r\n            {\r\n                return View();\r\n            }\r\n        }\r\n\r\n        \/\/\r\n        \/\/ GET: \/Ogrenci\/Edit\/5\r\n\r\n        public ActionResult Edit(int id)\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/\r\n        \/\/ POST: \/Ogrenci\/Edit\/5\r\n\r\n        &#x5B;HttpPost]\r\n        public ActionResult Edit(int id, FormCollection collection)\r\n        {\r\n            try\r\n            {\r\n                \/\/ TODO: Add update logic here\r\n\r\n                return RedirectToAction(&quot;Index&quot;);\r\n            }\r\n            catch\r\n            {\r\n                return View();\r\n            }\r\n        }\r\n\r\n        \/\/\r\n        \/\/ GET: \/Ogrenci\/Delete\/5\r\n\r\n        public ActionResult Delete(int id)\r\n        {\r\n            return View();\r\n        }\r\n\r\n        \/\/\r\n        \/\/ POST: \/Ogrenci\/Delete\/5\r\n\r\n        &#x5B;HttpPost]\r\n        public ActionResult Delete(int id, FormCollection collection)\r\n        {\r\n            try\r\n            {\r\n                \/\/ TODO: Add delete logic here\r\n\r\n                return RedirectToAction(&quot;Index&quot;);\r\n            }\r\n            catch\r\n            {\r\n                return View();\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Olu\u015fturdu\u011fumuz &#8220;Ogrenci(Controller)&#8221; ad\u0131ndaki Controller&#8217;\u0131m\u0131z\u0131 a\u00e7arsak e\u011fer, yukar\u0131daki kodlar\u0131 g\u00f6rece\u011fiz.Dikkat ederseniz e\u011fer, Index,Details,Create,Edit ve Delete ad\u0131nda be\u015f adet ActionResult tipinden metod ve baz\u0131lar\u0131n\u0131n overload hali eklenmi\u015ftir.<\/p>\n<p>Bu metodlardan biraz bahsetmek gerekirse e\u011fer,<\/p>\n<p><em><strong>Index<\/strong> <\/em>: Bir Controller olu\u015fturdu\u011fumuz zaman varsay\u0131lan olarak eklenen metod.<br \/>\n<strong><em>Details<\/em> <\/strong>: Detay i\u015flevi g\u00f6rmektedir.<br \/>\n<em><strong>Create<\/strong> <\/em>: Olu\u015fturma, ekleme gibi i\u015flemler i\u00e7in i\u015flev g\u00f6rmektedir.<br \/>\n<em><strong>Edit<\/strong> <\/em>: D\u00fczenleme i\u015flevi g\u00f6rmektedir.<br \/>\n<strong><em>Delete<\/em><\/strong> : Silme i\u015flevi g\u00f6rmektedir.<\/p>\n<p>Bu dizinin her bir yaz\u0131s\u0131nda bir metod ele al\u0131nacakt\u0131r.Bu dersimizde Index metodundan bahsedece\u011fiz.\u00d6nceden Index metodumuzda Model katman\u0131ndaki s\u0131n\u0131f\u0131m\u0131zdaki bir eleman\u0131n bilgisini g\u00f6steriyorduk.\u015eimdi bir listeleme(tablolama) i\u015flemi yapal\u0131m.<\/p>\n<p>Bu i\u015flemi yapabilmek i\u00e7in Model katman\u0131nda &#8220;Ogrenci&#8221; ad\u0131nda bir s\u0131n\u0131f olu\u015ftural\u0131m.(<span style=\"font-size: x-small;\"><strong><em>Bu s\u0131n\u0131f i\u00e7inde ID,Adi,SoyAdi,Yasi,Sinifi ve SinifHocasi ad\u0131nda propertyler eklensin.<\/em><\/strong><\/span>)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nnamespace Calisma.Models\r\n{\r\n    public class Ogrenci\r\n    {\r\n        public int ID { get; set; }\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public int Yasi { get; set; }\r\n        public int Sinifi { get; set; }\r\n        public string SinifHocasi { get; set; }\r\n\r\n        public Ogrenci()\r\n        { }\r\n\r\n        public Ogrenci(int ID, string Adi, string SoyAdi, int Yasi, int Sinifi, string SinifHocasi)\r\n        {\r\n            this.ID = ID;\r\n            this.Adi = Adi;\r\n            this.SoyAdi = SoyAdi;\r\n            this.Yasi = Yasi;\r\n            this.SinifHocasi = SinifHocasi;\r\n            this.Sinifi = Sinifi;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\u015eimdi bu Model katman\u0131ndaki &#8220;Ogrenci&#8221; s\u0131n\u0131f\u0131na &#8220;Ogrenci(Controller)&#8221; ad\u0131ndaki Controller&#8217;\u0131m\u0131zdan ula\u015fmak istiyorsak e\u011fer, bu Model katman\u0131n\u0131 Controller&#8217;\u0131m\u0131z\u0131n k\u00fct\u00fcphanesine eklememiz gerekiyor.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n--- OgrenciController.cs ---\r\nusing Calisma.Models;\r\n<\/pre>\n<p>\u015eimdi &#8220;Ogrenci(Controller)&#8221; s\u0131n\u0131f\u0131m\u0131zda Index metodunu ele al\u0131rsak e\u011fer, listeleme(tablolama) i\u015flemi yapaca\u011f\u0131m\u0131zdan dolay\u0131, veritaban\u0131 \u00fczerinden \u00e7al\u0131\u015fmad\u0131\u011f\u0131m\u0131z i\u00e7in Ogrenci tipinden List koleksiyonu olu\u015fturaca\u011f\u0131m ve bu koleksiyonun i\u00e7ine Ogrenci nesneleri ataca\u011f\u0131m.<br \/>\n(<span style=\"font-size: x-small;\"><em><strong>Not !!! Konumuza de\u011finirken, \u00f6rnekleri bu \u015fekilde prati\u011fe \u00e7evirmek biraz konuyu kompleksle\u015ftirmi\u015f olabilir.Ancak temel C#, Asp.NET bilgisi olanlar bu konuda s\u0131k\u0131nt\u0131 ya\u015famayacaklard\u0131r.<\/strong><\/em><\/span>)<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Index()\r\n        {\r\n            if (Session&#x5B;&quot;Ogrenciler&quot;] == null)\r\n            {\r\n                List&lt;Ogrenci&gt; Ogrenciler = new List&lt;Ogrenci&gt;() {\r\n             new Ogrenci(1,&quot;Gen\u00e7ay&quot;,&quot;Y\u0131ld\u0131z&quot;,21,2,&quot;Emrah \u00c7elebi&quot;),\r\n             new Ogrenci(2,&quot;Ahmet&quot;,&quot;Y\u0131ld\u0131r\u0131m&quot;,19,1,&quot;Ali Fuat G\u00f6ksu&quot;),\r\n             new Ogrenci(3,&quot;Fatih&quot;,&quot;Y\u0131ld\u0131z&quot;,22,3,&quot;Mehmet Ali El\u00e7evik&quot;)\r\n            };\r\n                Session&#x5B;&quot;Ogrenciler&quot;] = Ogrenciler;\r\n                return View(Ogrenciler);\r\n            }\r\n            else\r\n            {\r\n                return View(Session&#x5B;&quot;Ogrenciler&quot;] as List&lt;Ogrenci&gt;);\r\n            }\r\n        }\r\n<\/pre>\n<p>Yukar\u0131da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi &#8220;Index&#8221; ad\u0131ndaki metodumuzun i\u00e7eri\u011fini yazd\u0131k ve sonu\u00e7lar\u0131 View katman\u0131na g\u00f6nderdik.Kodlardan biraz s\u00f6z etmek gerekirse, Ogrenci tipinden bir List koleksiyonu olu\u015fturduk ve bu koleksiyon i\u00e7ine \u00fc\u00e7 adet Ogrenci nesnesi yerle\u015ftirip, bu koleksiyonumuzu &#8220;Ogrenciler&#8221; ad\u0131nda bir Session nesnemize att\u0131k.Gerekli Session \u00f6nlemlerinden sonra haliyle son olarak bu koleksiyonumuzu View katman\u0131na g\u00f6nderdik.<\/p>\n<p>\u015eimdi Index metodumuza sa\u011f t\u0131klayarak Add View diyelim ve bu metodumuzun View katman\u0131nda dosyas\u0131n\u0131 olu\u015ftural\u0131m.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/IndexView.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1044\" title=\"IndexView\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/IndexView.png\" alt=\"\" width=\"437\" height=\"431\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/IndexView.png 437w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/IndexView-300x295.png 300w\" sizes=\"auto, (max-width: 437px) 100vw, 437px\" \/><\/a><br \/>\nG\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, Index metodumuzun View dosyas\u0131n\u0131 olu\u015ftururken, listeleme yapaca\u011f\u0131m\u0131z i\u00e7in &#8220;View content&#8221; b\u00f6l\u00fcm\u00fcnden &#8220;<span style=\"color: #993366;\"><strong>List<\/strong><\/span>&#8221; se\u00e7ene\u011fini se\u00e7iyoruz.<\/p>\n<p>A\u015fa\u011f\u0131da Index.aspx sayfas\u0131n\u0131n kaynak kodlar\u0131 mevcuttur.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~\/Views\/Shared\/Site.Master&quot; Inherits=&quot;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;Calisma.Models.Ogrenci&gt;&gt;&quot; %&gt;\r\n&lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;\r\n\tIndex\r\n&lt;\/asp:Content&gt;\r\n&lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;\r\n    &lt;h2&gt;Index&lt;\/h2&gt;\r\n    &lt;table&gt;\r\n        &lt;tr&gt;\r\n            &lt;th&gt;&lt;\/th&gt;\r\n            &lt;th&gt;\r\n                ID\r\n            &lt;\/th&gt;\r\n            &lt;th&gt;\r\n                Adi\r\n            &lt;\/th&gt;\r\n            &lt;th&gt;\r\n                SoyAdi\r\n            &lt;\/th&gt;\r\n            &lt;th&gt;\r\n                Yasi\r\n            &lt;\/th&gt;\r\n            &lt;th&gt;\r\n                Sinifi\r\n            &lt;\/th&gt;\r\n            &lt;th&gt;\r\n                SinifHocasi\r\n            &lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n    &lt;% foreach (var item in Model) { %&gt;\r\n\/\/Model art\u0131k i\u00e7inde bir List koleksiyon ta\u015f\u0131makta oldu\u011fundan dolay\u0131 direkt olarak foreach d\u00f6ng\u00fcs\u00fcnde kullan\u0131l\u0131yor.\r\n        &lt;tr&gt;\r\n            &lt;td&gt;\r\n                &lt;%: Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new { id = item.ID}) %&gt; |\r\n                &lt;%: Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id = item.ID })%&gt; |\r\n                &lt;%: Html.ActionLink(&quot;Delete&quot;, &quot;Delete&quot;, new { id = item.ID })%&gt;\r\n\/\/Burada id parametresine her nesnenin ID de\u011ferini verdim.\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.ID %&gt;\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.Adi %&gt;\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.SoyAdi %&gt;\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.Yasi %&gt;\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.Sinifi %&gt;\r\n            &lt;\/td&gt;\r\n            &lt;td&gt;\r\n                &lt;%: item.SinifHocasi %&gt;\r\n            &lt;\/td&gt;\r\n        &lt;\/tr&gt;\r\n    &lt;% } %&gt;\r\n    &lt;\/table&gt;\r\n    &lt;p&gt;\r\n        &lt;%: Html.ActionLink(&quot;Create New&quot;, &quot;Create&quot;) %&gt;\r\n    &lt;\/p&gt;\r\n&lt;\/asp:Content&gt;\r\n<\/pre>\n<p>Index.aspx sayfas\u0131n\u0131n kaynak kodlar\u0131nda g\u00f6rd\u00fc\u011f\u00fcn\u00fcz bu yap\u0131 ve tasar\u0131m, \u015fimdiki projemizde varsay\u0131lan olarak gelmektedirler.\u0130lerideki yaz\u0131lar\u0131mda bu tasar\u0131m ve yap\u0131lar\u0131 kendimiz belirleyip olu\u015fturabilece\u011fiz.<\/p>\n<p>Projemizi bu haliyle \u00e7al\u0131\u015ft\u0131r\u0131p, adres \u00e7ubu\u011funa &#8220;http:\/\/localhost:****\/Ogrenci&#8221; ifadesini yazarsak e\u011fer, a\u015fa\u011f\u0131daki g\u00f6r\u00fcnt\u00fc kar\u015f\u0131m\u0131za gelecektir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1046\" title=\"list\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/list.png\" alt=\"\" width=\"510\" height=\"192\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/list.png 510w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/list-300x112.png 300w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/a><\/p>\n<p>Bu yaz\u0131m\u0131z\u0131nda sonuna gelmi\u015f bulunmaktay\u0131z.<br \/>\nBir sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere..<br \/>\n\u0130yi \u00c7al\u0131\u015fmalar..<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Bu yaz\u0131 dizimizde MVC projemizde Controller olu\u015ftururken &#8220;Add action methods for Create, Update, Delete and Details scenarios&#8221; ifadesinden bahsedelim. Bu yaz\u0131 dizimizde, bu g\u00fcne kadar bahsetti\u011fim MVC konular\u0131m\u0131z\u0131 kapsayan, ayr\u0131nt\u0131l\u0131 ve teferruatl\u0131 bir \u00f6rnek&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[746,293,87,292,294,283],"class_list":["post-1036","post","type-post","status-publish","format-standard","hentry","category-asp-net-mvc","tag-asp-net-mvc","tag-create","tag-delete","tag-details","tag-edit","tag-mvc"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=1036"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1036\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}