﻿
{"id":1027,"date":"2012-11-24T01:27:53","date_gmt":"2012-11-24T01:27:53","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=1027"},"modified":"2012-11-24T01:27:53","modified_gmt":"2012-11-24T01:27:53","slug":"mvcde-model-olusturma","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/mvcde-model-olusturma\/","title":{"rendered":"MVC&#8217;de Model Olu\u015fturma"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Bu yaz\u0131m\u0131za kadar, Asp.NET MVC katmanlar\u0131ndan Controller ve View katmanlar\u0131n\u0131 inceledik.\u015eimdi s\u0131ra geldi Model katman\u0131n\u0131 tan\u0131maya.<br \/>\nModel, veritaban\u0131ndaki tablolar\u0131n Class yap\u0131s\u0131na d\u00f6n\u00fc\u015ft\u00fcr\u00fclm\u00fc\u015f ve veritaban\u0131ndaki kolonlar\u0131n s\u0131n\u0131flarda propertyle\u015fmi\u015f olarak Entity yap\u0131s\u0131na d\u00f6n\u00fc\u015ft\u00fcr\u00fclm\u00fc\u015f halidir diyebiliriz.<\/p>\n<p>Visual Studio&#8217;da herhangi bir isimde Asp.NET MVC projesi \u00fczerinden \u00e7al\u0131\u015ft\u0131\u011f\u0131m\u0131z\u0131 varsayarsak e\u011fer, veritaban\u0131m\u0131zda &#8220;Insanlar&#8221; tablosunu Model katman\u0131nda s\u0131n\u0131f olarak modelleyelim.<br \/>\n<strong><em><span style=\"font-size: x-small;\">(Herhangi bir veritaban\u0131nda Insanlar ad\u0131nda bir tablo oldu\u011funu ve i\u00e7inde ID,Ad,Soyad,DogumTarihi bilgilerini tutan kolonlar oldu\u011funu varsayal\u0131m.)<\/span><\/em><\/strong><\/p>\n<p>Solution Explorer penceresinde Models isimli klas\u00f6re sa\u011f t\u0131k, Add ve New Item sekmesinden Class se\u00e7ip, Class ismi olarak &#8220;Insan&#8221; ad\u0131n\u0131 yazd\u0131ktan sonra Add butonuna bas\u0131yoruz.<\/p>\n<p>Model katman\u0131m\u0131z i\u00e7inde Insan.cs ad\u0131nda bir s\u0131n\u0131f olu\u015fturmu\u015f olduk.Bu s\u0131n\u0131f\u0131n i\u00e7ine veritaban\u0131m\u0131zdaki kolonlar\u0131n property olarak tutulacaklar\u0131n\u0131 yaz\u0131yoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nnamespace ModelGiris.Models\r\n{\r\npublic class Insan\r\n{\r\npublic int ID { get; set; }\r\npublic string Ad { get; set; }\r\npublic string Soyad { get; set; }\r\npublic string DogumTarihi { get; set; }\r\n}\r\n}\r\n<\/pre>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, Insanlar tablosunda varsayd\u0131\u011f\u0131m\u0131z kolonlar\u0131 property olarak &#8220;Insan&#8221; s\u0131n\u0131f\u0131na yazm\u0131\u015f bulunmaktay\u0131z.<\/p>\n<p>\u015eimdi bu Model katman\u0131ndaki Insan.cs s\u0131n\u0131f\u0131n\u0131, Controller katman\u0131ndaki herhangi bir s\u0131n\u0131f\u0131n da kullanarak View katman\u0131na o propertylere girilen verileri g\u00f6sterebiliriz.<\/p>\n<p>O halde Controller katman\u0131nda &#8220;Insan(Controller)&#8221; ad\u0131nda bir Controller s\u0131n\u0131f\u0131 olu\u015fturuyorum.Ayn\u0131 \u015fekilde bu Insan s\u0131n\u0131f\u0131n\u0131n View elemanlar\u0131n\u0131da otomatik derleyiciye olu\u015ftutturuyorum.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/view.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1032\" title=\"view\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/view.png\" alt=\"\" width=\"433\" height=\"425\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/view.png 433w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/view-300x294.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/a>Model katman\u0131yla i\u015fimiz oldu\u011fundan dolay\u0131, &#8220;Create a strongly-typed view&#8221; kutusunu se\u00e7ip, &#8220;View data class&#8221; alan\u0131ndan &#8220;Insan&#8221; ad\u0131ndaki Model&#8217;\u0131 se\u00e7iyoruz.&#8221;View content&#8221; Empty olarak se\u00e7iyoruz ve Add diyoruz.<\/p>\n<p>&#8220;Insan(Controller)&#8221; ad\u0131ndaki Controller&#8217;\u0131m\u0131z\u0131n i\u00e7inde &#8220;Index&#8221; ad\u0131nda varsay\u0131lan bir ActionResult tipinden bir metodumuz mevcut.Bu metodumuzun View&#8217;\u0131ndaki sayfas\u0131nda ben Model katman\u0131m\u0131zdaki Insan s\u0131n\u0131f\u0131nda olan propertylerin de\u011ferlerinin bir \u015fekilde g\u00f6z\u00fckmesini istiyorum.Tabi bu propertyleri &#8220;Index&#8221; metodunda doldurmal\u0131 ve View katman\u0131na bu Insan isminde olan s\u0131n\u0131f\u0131n yani modelin nesnesini g\u00f6ndermeliyiz.<\/p>\n<p>Model katman\u0131nda olu\u015fturdu\u011fumuz Insan s\u0131n\u0131f\u0131ndan bir nesne \u00fcretilerek \u00f6ncelikle Action metod i\u00e7inde i\u015flemlerimizi yapal\u0131m.<\/p>\n<p>Dikkat ederseniz, &#8220;Index&#8221; ismindeki metodumuzun scopelar\u0131 i\u00e7inde Insan yazd\u0131\u011f\u0131m\u0131z zaman, Model katman\u0131m\u0131zdaki &#8220;Insan&#8221; s\u0131n\u0131f\u0131na ula\u015famamaktay\u0131z.Buna ula\u015fmak i\u00e7in Model katman\u0131n\u0131 Namespace&#8217;i ile sayfam\u0131za using etmeliyiz.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Model.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1031\" title=\"Model\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Model.png\" alt=\"\" width=\"318\" height=\"400\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Model.png 318w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/Model-238x300.png 238w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/a><\/p>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi, Model katman\u0131n\u0131 Namespace&#8217;i ile using ettikten sonra, Model katman\u0131n\u0131n i\u00e7indeki Insan ismindeki s\u0131n\u0131fa ula\u015fabildik ve bir Instance&#8217;\u0131n\u0131 alabildik.Bu nesne \u00fczerinden de, Insan s\u0131n\u0131f\u0131n\u0131n b\u00fct\u00fcn propertylerine ulabildik.Eee haliyle bu propertylere de\u011ferler girebilece\u011fiz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\u00a0 \u00a0 \u00a0 \u00a0 public ActionResult Index()\r\n\u00a0 \u00a0 \u00a0 \u00a0 {\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Insan insan = new Insan();\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 insan.ID = 1;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 insan.Ad = &quot;Gen\u00e7ay&quot;;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 insan.Soyad = &quot;Y\u0131ld\u0131z&quot;;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 insan.DogumTarihi = &quot;09.05.1992&quot;;\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 return View(insan);\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/insan nesnesini View katman\u0131na g\u00f6nderiyoruz.Bu sayede View katman\u0131nda bu nesneye ulabilece\u011fiz.\r\n\u00a0 \u00a0 \u00a0 \u00a0 }\r\n<\/pre>\n<p>View katman\u0131nda &#8220;Insan&#8221; klas\u00f6r\u00fc alt\u0131nda, Index.aspx dosyas\u0131nda, propertylere girilen de\u011ferleri a\u015fa\u011f\u0131daki gibi g\u00f6sterebiliriz.<\/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;ModelGiris.Models.Insan&gt;&quot; %&gt;\r\n&lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;\r\n    Index\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;p&gt;\r\n        ID :\r\n        &lt;%: Model.ID %&gt;\r\n    &lt;\/p&gt;\r\n    &lt;p&gt;\r\n        Ad :\r\n        &lt;%:Model.Ad %&gt;\r\n    &lt;\/p&gt;\r\n    &lt;p&gt;\r\n        Soyad :\r\n        &lt;%:Model.Soyad %&gt;\r\n    &lt;\/p&gt;\r\n    &lt;p&gt;\r\n        Do\u011fum Tarihi :\r\n        &lt;%: Model.DogumTarihi %&gt;\r\n    &lt;\/p&gt;\r\n&lt;\/asp:Content&gt;\r\n<\/pre>\n<p>En nihayetinde, Insan s\u0131n\u0131f\u0131n\u0131n propertylerindeki de\u011ferleri g\u00f6sterece\u011fiz.Peki bu Insan s\u0131n\u0131f\u0131 bir Model de\u011fil mi?Evet, bir Model oldu\u011fundan dolay\u0131, &#8220;<%: %>&#8221; i\u00e7inde &#8220;Model&#8221; kodunu kullan\u0131rsak e\u011fer, &#8220;Insan(Controller)&#8221; ad\u0131ndaki Controller&#8217;\u0131m\u0131z i\u00e7indeki &#8220;Index&#8221; adl\u0131 ActionResult tipinden olan metodun i\u00e7inde olu\u015fturulup, propertylerine de\u011fer atanan Insan tipinden &#8220;insan&#8221; ad\u0131ndaki nesnemiz View katman\u0131na g\u00f6nderildi\u011finden bu kodda o tipin nesnesi, ve o nesnenin de\u011fer dolu propertylerine eri\u015fmi\u015f oluruz.Bunu sa\u011flayan Index.aspx sayfas\u0131n\u0131n kaynak kodlar\u0131ndaki en \u00fcst sat\u0131rda yazan, Inherits blo\u011fudur.<\/p>\n<p>Sonu\u00e7 olarak ekranda propertymizdeki de\u011ferler g\u00f6z\u00fckmektedir.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/EkranCiktisi.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2012\/11\/EkranCiktisi.png\" alt=\"\" title=\"EkranCiktisi\" width=\"199\" height=\"168\" class=\"aligncenter size-full wp-image-1034\" \/><\/a><\/p>\n<p>Bu yaz\u0131m\u0131z\u0131nda sonuna gelmi\u015f bulunmaktay\u0131z.<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>Bu yaz\u0131m\u0131za kadar, Asp.NET MVC katmanlar\u0131ndan Controller ve View katmanlar\u0131n\u0131 inceledik.\u015eimdi s\u0131ra geldi Model katman\u0131n\u0131 tan\u0131maya. Model, veritaban\u0131ndaki tablolar\u0131n Class yap\u0131s\u0131na d\u00f6n\u00fc\u015ft\u00fcr\u00fclm\u00fc\u015f ve veritaban\u0131ndaki kolonlar\u0131n s\u0131n\u0131flarda propertyle\u015fmi\u015f olarak Entity yap\u0131s\u0131na d\u00f6n\u00fc\u015ft\u00fcr\u00fclm\u00fc\u015f halidir diyebiliriz. Visual&#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,284,283],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-asp-net-mvc","tag-asp-net-mvc","tag-model","tag-mvc"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1027","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=1027"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}