﻿
{"id":1058,"date":"2012-11-30T01:44:53","date_gmt":"2012-11-30T01:44:53","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=1058"},"modified":"2019-02-08T21:22:00","modified_gmt":"2019-02-08T21:22:00","slug":"mvcde-create-metodu","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/mvcde-create-metodu\/","title":{"rendered":"MVC&#8217;de Create Metodu"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Bir \u00f6nceki, MVC\u2019de Details Metodu ba\u015fl\u0131kl\u0131 yaz\u0131m\u0131zda MVC&#8217;de detay(Details) i\u015flemlerini g\u00f6rm\u00fc\u015ft\u00fck.Bu yaz\u0131m\u0131zda ise, Create metodunu, yani ekleme i\u015flemini g\u00f6rece\u011fiz.<br \/>\n<!--more--><br \/>\n<span style=\"font-size: x-small;\"><strong>Dikkat !!!<\/strong><\/span><br \/>\n<span style=\"font-size: xx-small;\"><strong>Bu yaz\u0131m\u0131z\u0131 anlaman\u0131z i\u00e7in bir \u00f6nceki yaz\u0131m\u0131z\u0131 detayl\u0131 bir bi\u00e7imde okuman\u0131z gerekmektedir.Yaz\u0131 dizisi olu\u015fturdu\u011fumdan dolay\u0131, bir yaz\u0131 \u00f6nceki ve sonrakiyle ba\u011flant\u0131l\u0131 \u015fekilde \u00f6rneklendirilmi\u015ftir.<\/strong><\/span><\/p>\n<p>An\u0131msarsan\u0131z e\u011fer bir \u00f6nceki yaz\u0131m\u0131zda neler yapm\u0131\u015ft\u0131k onlardan bahsedelim.\u201dOgrenci(Controller)\u201d ad\u0131ndaki Controller s\u0131n\u0131f\u0131m\u0131zda bulunan, ActionResult tipinden olan &#8220;Details&#8221; isimli metodumuzu kullanarak, listeletti\u011fimiz \u00f6\u011frencilerin detaylar\u0131n\u0131 haz\u0131rlam\u0131\u015ft\u0131k.Bu yaz\u0131m\u0131zda ise, olu\u015fturdu\u011fumuz bu listeye bir veri ekleme i\u015flemi yapaca\u011f\u0131z.<\/p>\n<p>\u00d6ncelikle, \u00fczerinde \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u0131z projemizi a\u00e7\u0131p, projemizi ba\u015flatal\u0131m.Adres \u00e7ubu\u011funa &#8220;http:\/\/localhost:****\/Ogrenci&#8221; benzer ifademizi girelim.A\u00e7\u0131lan sayfada a\u015fa\u011f\u0131daki gibi, varsay\u0131lan tasar\u0131mdan \u00f6t\u00fcr\u00fc gelen bir &#8220;Create New&#8221; linki mevcuttur.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/create.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1059\" title=\"create\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/create.png\" alt=\"\" width=\"524\" height=\"140\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/create.png 524w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/create-300x80.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\" \/><\/a>\u00a0Bu link \u00fczerine geldi\u011fimizde taray\u0131c\u0131n\u0131n sol alt taraf\u0131nda &#8221;\u00a0http:\/\/localhost:****\/Ogrenci\/Create&#8221; URL sini g\u00f6rece\u011fiz.Bu linkin a\u00e7\u0131klamas\u0131 neydi.&#8221;Ogrenci&#8221; ad\u0131ndaki Controller s\u0131n\u0131f\u0131n\u0131n i\u00e7indeki &#8220;Create&#8221; ad\u0131ndaki ActionResult tipinden metodu temsil ediyor(\u00e7al\u0131\u015ft\u0131r\u0131yor).<\/p>\n<p>Birde Controller katman\u0131m\u0131zdaki &#8220;Ogrenci(Controller)&#8221; s\u0131nf\u0131m\u0131zdaki &#8220;Create&#8221; metoduna bir g\u00f6z atal\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/1.Metod\r\n        public ActionResult Create()\r\n        {\r\n            return View();\r\n        }\r\n\/\/2.Metod\r\n        &#x5B;HttpPost]\r\n        public ActionResult Create(FormCollection collection)\r\n        {\r\n            try\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<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, iki adet Create isminde overload metod mevcut.\u015eimdi buradaki hususu iyi okuman\u0131z\u0131 tavsiye ederim.<br \/>\nCreate metodu ile yeni bir ekleme i\u015flemi yapaca\u011f\u0131m\u0131z zaman(\u00f6rne\u011fimizde \u00f6\u011frenci ekleyece\u011fiz), ekleyece\u011fimiz eleman\u0131n nesnesini \u00fcretip bunu kullan\u0131c\u0131ya g\u00f6ndertmemiz gerekiyor.Bu i\u015flem sonucunda, g\u00f6nderdi\u011fimiz bu nesnenin gerekli alanlar\u0131n\u0131 kullan\u0131c\u0131 doldurup geri bize post etmesi gerekiyor.<\/p>\n<p>Yukar\u0131da isimlendirdi\u011fim 1.Metod ad\u0131ndaki Create metodu, &#8220;Create New&#8221; linkine t\u0131klad\u0131\u011f\u0131 zaman \u00f6nce \u00e7al\u0131\u015facakt\u0131r.Bu metod \u00e7al\u0131\u015ft\u0131\u011f\u0131 ekleme i\u015flemi yap\u0131lacak nesne \u00fcretilecek ve bu kullan\u0131c\u0131ya g\u00f6nderilecektir.Daha sonra kullan\u0131c\u0131 gerekli alanlar\u0131 doldurduktan sonra post i\u015flemi yapacakt\u0131r ve 2.Metod ad\u0131ndaki Create metodumuz \u00e7al\u0131\u015f\u0131p, gerekli ekleme i\u015flemlerini halledecektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;HttpPost]\r\n<\/pre>\n<p>Attribute&#8217;u form taraf\u0131ndan post tipinden bir Action olu\u015ftu\u011fu zaman, 2.Metod ismindeki Create metodunun \u00e7al\u0131\u015faca\u011f\u0131n\u0131 s\u00f6ylemektedir.<\/p>\n<p>\u015eimdi bahsetti\u011fimiz gibi \u00f6ncelikle, kullan\u0131c\u0131n\u0131n dolduraca\u011fu nesnemizi 1.Metod ad\u0131ndaki Create overload\u0131nda olu\u015fturup, bu nesneyi View katman\u0131na g\u00f6ndermeliyiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Create()\r\n        {\r\n            Ogrenci o = new Ogrenci();\r\n            return View(o);\r\n        }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, 1.Metod ad\u0131ndaki Create metodunda Ogrenci tipinden bir nesne olu\u015fturduk ve kullan\u0131c\u0131n\u0131n bu nesnedeki gerekli alanlara bilgi girmesi i\u00e7in nesnemizi View katman\u0131na g\u00f6nderdik.<\/p>\n<p>Bu i\u015flemi yapt\u0131ktan sonra, Create metodumuza sa\u011f t\u0131klay\u0131p &#8220;Add View&#8221; se\u00e7ene\u011fini se\u00e7elim ve gelen pencereden a\u015fa\u011f\u0131daki gibi ayarlar yapt\u0131ktan sonra Add se\u00e7ene\u011fine t\u0131klayal\u0131m.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/createview.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1060\" title=\"createview\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/createview.png\" alt=\"\" width=\"438\" height=\"430\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/createview.png 438w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/createview-300x294.png 300w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/a><\/p>\n<p>Ekleme i\u015flemi yapaca\u011f\u0131m\u0131z i\u00e7in &#8220;View content&#8221; b\u00f6l\u00fcm\u00fcnden Create se\u00e7ene\u011fini se\u00e7meyi unutmay\u0131n\u0131z.<\/p>\n<p>Bu i\u015flem sonucunda olu\u015fan Create.aspx sayfas\u0131n\u0131n kaynak kodlar\u0131 a\u015fa\u011f\u0131daki gibi olacakt\u0131r.<\/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;Calisma.Models.Ogrenci&gt;&quot; %&gt;\r\n&lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;\r\n\tCreate\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;Create&lt;\/h2&gt;\r\n    &lt;% using (Html.BeginForm()) {%&gt;\r\n        &lt;%: Html.ValidationSummary(true) %&gt;\r\n        &lt;fieldset&gt;\r\n            &lt;legend&gt;Fields&lt;\/legend&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.ID) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.ID) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.ID) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.Adi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.Adi) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.Adi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.SoyAdi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.SoyAdi) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.SoyAdi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.Yasi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.Yasi) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.Yasi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.Sinifi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.Sinifi) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.Sinifi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-label&quot;&gt;\r\n                &lt;%: Html.LabelFor(model =&gt; model.SinifHocasi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;div class=&quot;editor-field&quot;&gt;\r\n                &lt;%: Html.TextBoxFor(model =&gt; model.SinifHocasi) %&gt;\r\n                &lt;%: Html.ValidationMessageFor(model =&gt; model.SinifHocasi) %&gt;\r\n            &lt;\/div&gt;\r\n            &lt;p&gt;\r\n                &lt;input type=&quot;submit&quot; value=&quot;Create&quot; \/&gt;\r\n            &lt;\/p&gt;\r\n        &lt;\/fieldset&gt;\r\n    &lt;% } %&gt;\r\n    &lt;div&gt;\r\n        &lt;%: Html.ActionLink(&quot;Back to List&quot;, &quot;Index&quot;) %&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/asp:Content&gt;\r\n<\/pre>\n<p>E\u011fer projemizi \u00e7al\u0131\u015ft\u0131r\u0131p taray\u0131c\u0131m\u0131za &#8220;http:\/\/localhost:****\/Ogrenci\/&#8221; benzer bir link girerek, kar\u015f\u0131m\u0131za gelen pencereden &#8220;Create New&#8221; linkine t\u0131klad\u0131\u011f\u0131m\u0131zda, ekran g\u00f6r\u00fcnt\u00fcs\u00fc a\u015fa\u011f\u0131daki gibi olacakt\u0131r.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/cc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1062\" title=\"cc\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/cc.png\" alt=\"\" width=\"213\" height=\"336\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/cc.png 213w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/cc-190x300.png 190w\" sizes=\"auto, (max-width: 213px) 100vw, 213px\" \/><\/a><\/p>\n<p>\u015eimdi ise, 2.Metod ismindeki Create overload\u0131n\u0131 inceleyelim.Daha detayl\u0131 incelemek i\u00e7in bu metodu a\u015fa\u011f\u0131ya tek olarak al\u0131yorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;HttpPost]\r\n        public ActionResult Create(FormCollection collection)\r\n        {\r\n            try\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<\/pre>\n<p>\u00d6ncelikle buraya kadar mekanizmam\u0131z\u0131n \u00e7al\u0131\u015fma mant\u0131\u011f\u0131n\u0131 bir izleyelim.1.Metod ad\u0131ndaki Create overload\u0131 ile olu\u015fturulan Ogrenci tipindeki nesnemiz kullan\u0131c\u0131ya g\u00f6nderildi ve gerekli alanlar\u0131na kullan\u0131c\u0131 taraf\u0131ndan veriler doldurtturuldu.<\/p>\n<p>Kullan\u0131c\u0131 gerekli verileri girdikten sonra, Create butonuna t\u0131klayacak, bizim olu\u015fturup g\u00f6nderdi\u011fimiz Ogrenci nesnesini alanlar\u0131 doldurulmu\u015f bir \u015fekilde 2.Metod ismindeki Create metoduna geri g\u00f6nderecektir.[HttpPost] attribute u bu metodun post i\u015flemi sonucunda devreye girece\u011fini g\u00f6steriyor.<\/p>\n<p>Gelen nesneyi, 2.Metod isimli Create overload\u0131 FormCollection tipinden olan &#8220;collection&#8221; ismindeki parametre yakalayacakt\u0131r.Mant\u0131ken biz kullanc\u0131ya bir Ogrenci tipinden bir nesne g\u00f6ndermi\u015ftik.Eee haliyle kullan\u0131c\u0131da bize o nesneyi, verileri yerle\u015ftirilmi\u015f bir \u015fekilde geri g\u00f6nderdi.Yani, piyasada bir tane Ogrenci nesnesi mevcutken, Create FormCollection tipinden parametreyle bu nesneyi yakalayaca\u011f\u0131ma, direkt Ogrenci tipinden parametreyle yakalar\u0131m daha iyi.<\/p>\n<p>O halde ben 2.Metod isimli Create overload\u0131n\u0131, Ogrenci tipinden parametre alan bir metod haline getiriyorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;HttpPost]\r\n        public ActionResult Create(Ogrenci ogrenci)\r\n        {\r\n            try\r\n            {\r\n                return RedirectToAction(&quot;Index&quot;);\r\n\/\/Index ad\u0131ndaki Action'a y\u00f6nlendir.\r\n\r\n            }\r\n            catch\r\n            {\r\n                return View();\r\n            }\r\n        }\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi Ogrenci tipinden &#8220;ogrenci&#8221; ad\u0131nda bir parametre alan metod haline getirdim.Art\u0131k bizim, Ogrenci tipindeki nesnemizi bu parametre yakalayacakt\u0131r.\u015eimdi ise bu Ogrenci tipinden nesnemizi Ogrenci tipinden List koleksiyonumuza atabiliriz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;HttpPost]\r\n        public ActionResult Create(Ogrenci ogrenci)\r\n        {\r\n            try\r\n            {\r\n                List&lt;Ogrenci&gt; Ogrencilerimiz = Session&#x5B;&quot;Ogrenciler&quot;] as List&lt;Ogrenci&gt;;\r\n                Ogrencilerimiz.Add(ogrenci);\r\n                return RedirectToAction(&quot;Index&quot;);\r\n            }\r\n            catch\r\n            {\r\n                return View();\r\n            }\r\n        }\r\n<\/pre>\n<p>Art\u0131k projemizi \u00e7al\u0131\u015ft\u0131rd\u0131ktan sonra, &#8220;Create New&#8221; linkine gelerek gerekli alanlara gerekli bilgileri girdikten sonra Create butonuna t\u0131klayarak, verilerimizin listeye eklendi\u011fini g\u00f6rece\u011fiz.<\/p>\n<p>Bu yaz\u0131m\u0131zda MVC&#8217;de Create i\u015flemi nas\u0131l yap\u0131l\u0131r g\u00f6rm\u00fc\u015f olduk.Faydalanman\u0131z dile\u011fiyle,<br \/>\nBir sonraki yaz\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere..<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/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>Bir \u00f6nceki, MVC\u2019de Details Metodu ba\u015fl\u0131kl\u0131 yaz\u0131m\u0131zda MVC&#8217;de detay(Details) i\u015flemlerini g\u00f6rm\u00fc\u015ft\u00fck.Bu yaz\u0131m\u0131zda ise, Create metodunu, yani ekleme i\u015flemini g\u00f6rece\u011fiz.<!-- 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,283],"class_list":["post-1058","post","type-post","status-publish","format-standard","hentry","category-asp-net-mvc","tag-asp-net-mvc","tag-create","tag-mvc"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1058","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=1058"}],"version-history":[{"count":1,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1058\/revisions"}],"predecessor-version":[{"id":10378,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1058\/revisions\/10378"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=1058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=1058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=1058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}