﻿
{"id":3194,"date":"2015-08-18T09:16:23","date_gmt":"2015-08-18T09:16:23","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=3194"},"modified":"2015-08-18T09:17:40","modified_gmt":"2015-08-18T09:17:40","slug":"asp-net-mvc-bir-viewda-tuple-ile-coklu-model-kullanimi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/asp-net-mvc-bir-viewda-tuple-ile-coklu-model-kullanimi\/","title":{"rendered":"Asp.NET MVC &#8211; Bir View&#8217;da Tuple S\u0131n\u0131f\u0131 \u0130le \u00c7oklu Model Kullan\u0131m\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Y\u00fcr\u00fcyorsan ko\u015fabilmelisin, ko\u015fuyorsan u\u00e7abilmelisin, u\u00e7uyorsan \u0131\u015f\u0131nlanabilmelisin.Durmamal\u0131s\u0131n.Bu g\u00fcn\u00fcn, d\u00fcn\u00fcnden bir ad\u0131m ileri olmal\u0131, tarihle yar\u0131\u015fmal\u0131, yar\u0131na daha \u00e7etin bir maratonla ba\u015flamal\u0131s\u0131n.Bu ya\u015fad\u0131\u011f\u0131m\u0131z \u00e7a\u011f\u0131n gereksinimlerindendir.\u00d6yleyse i\u015finde de \u00e7\u0131\u011f\u0131r a\u00e7mal\u0131s\u0131n.Evet, bu yaz\u0131m\u0131zda Asp.NET MVC&#8217;de (\u015fahsen)mimarisel mant\u0131\u011f\u0131 a\u015fmak i\u00e7in \u00e7abalayaca\u011f\u0131z.<strong>Peki nedir bu yaz\u0131m\u0131z\u0131n i\u00e7eri\u011fi?<\/strong> diye sorarsan\u0131z e\u011fer hemen izah edeyim.Biliyorsunuz ki, Asp.NET MVC mant\u0131\u011f\u0131 kullan\u0131c\u0131dan gelen(View) talep Controller&#8217;da gerekli veritaban\u0131 i\u015flemleri(Model) ile i\u015flenir ve geri kullan\u0131c\u0131ya yani View&#8217;a cevap g\u00f6nderilir.Bu genel mimarisel yap\u0131da, View katman\u0131nda sadece bir adet Model kullanma hakk\u0131m\u0131z vard\u0131r.Tabi ki de, binbir t\u00fcrl\u00fc teknikle birden fazla model kullanabilme \u015ferefine nail olabiliriz ama ben \u015fuana kadar g\u00f6rd\u00fc\u011f\u00fcm en g\u00f6zde tekni\u011fi sizlerle payla\u015faca\u011f\u0131m.Haliyle bu profesyonel teknik sayesinde Asp.NET MVC mimarisinde ki bu s\u0131n\u0131r\u0131 a\u015fm\u0131\u015f oldu\u011fumuzu d\u00fc\u015f\u00fcnd\u00fc\u011f\u00fcm i\u00e7in yaz\u0131m\u0131za b\u00f6yle bir heyecanla giri\u015f yapmak istedim.<\/p>\n<p>Tuple s\u0131n\u0131f\u0131 ile bir View&#8217;a birden fazla model tan\u0131mlayabiliriz.Tabi, mevzuya giri\u015f yapmadan \u00f6nce neden stabil tekniklerin i\u015fe yaramad\u0131\u011f\u0131n\u0131 izaha \u00f6ncelik tan\u0131yaca\u011f\u0131z.<\/p>\n<p>Makalemizde \u00f6rneklendirme ama\u00e7l\u0131 a\u015fa\u011f\u0131daki &#8220;Okul&#8221; ve &#8220;Ogretmen&#8221; isimli iki modeli ba\u015ftan olu\u015fturuyorum.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Okul\r\n    {\r\n        public string OkulAdi { get; set; }\r\n    }\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Ogretmen\r\n    {\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public string Bransi { get; set; }\r\n        public double Maasi { get; set; }\r\n    }\r\n<\/pre>\n<p>Senaryomuz \u015f\u00f6yle olacakt\u0131r.Bir formda \u00f6\u011fretmenle ilgili bilgileri post ederken okul bilgiside post edilecektir lakin \u00f6\u011fretmenle ilgili bilgileri &#8220;Ogretmen&#8221; isimli, okul bilgilerini ise &#8220;Okul&#8221; isimli modelimiz tutacakt\u0131r.Bu i\u015flemi ger\u00e7ekle\u015ftirebilmemiz i\u00e7in View&#8217;da iki adet model tan\u0131mlamas\u0131 yapmam\u0131z gerekmektedir.Tabi bu teknik olarak m\u00fcmk\u00fcn olmad\u0131\u011f\u0131 i\u00e7in \u015fimdi sizlere basit mant\u0131k tekni\u011fi g\u00f6sterece\u011fim ve bu tekni\u011fin her zaman i\u015fe yaramad\u0131\u011f\u0131n\u0131 anlatmaya \u00e7al\u0131\u015faca\u011f\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n@using WebApplication14.Models\r\n\r\n@model Ogretmen\r\n\r\n@using (Html.BeginForm(&quot;Post&quot;, &quot;Home&quot;, FormMethod.Post))\r\n{\r\n    @:Ad\u0131:\r\n    @Html.TextBoxFor(model =&gt; model.Adi)\r\n    &lt;br \/&gt;\r\n    @:Soyad\u0131:\r\n    @Html.TextBoxFor(model =&gt; model.SoyAdi)\r\n    &lt;br \/&gt;\r\n    @:Maa\u015f\u0131:\r\n    @Html.TextBoxFor(model =&gt; model.Maasi)\r\n    &lt;br \/&gt;\r\n    @:Bran\u015f\u0131:\r\n    @Html.TextBoxFor(model =&gt; model.Bransi)\r\n    &lt;br \/&gt;\r\n    @:Okulu:\r\n    @Html.TextBoxFor(model =&gt; model.Okul.OkulAdi)\r\n\r\n    &lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; \/&gt;\r\n}\r\n<\/pre>\n<p>Yukar\u0131daki View dosyas\u0131na bakarsan\u0131z e\u011fer \u00f6\u011fretmen formu olu\u015fturulmu\u015ftur.Ayriyetten Ogretmen modeli \u00fczerinden Okul tipinde bir property tan\u0131mlanm\u0131\u015f ve o \u00f6zellikle de okul bilgisi tutulmaya \u00e7al\u0131\u015f\u0131lm\u0131\u015ft\u0131r.<\/p>\n<p>Bu i\u015flemi yapabilmek i\u00e7in Ogretmen modeline a\u015fa\u011f\u0131daki kod ekini yapmam\u0131zy yeterlidir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class Ogretmen\r\n    {\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public string Bransi { get; set; }\r\n        public double Maasi { get; set; }\r\n\r\n        public Okul Okul { get; set; }\r\n    }\r\n<\/pre>\n<p>Bu \u015fekilde kullan\u0131lan bir model tasar\u0131m\u0131nda bakal\u0131m nas\u0131l bir durumla kar\u015f\u0131 kar\u015f\u0131ya kal\u0131yoruz.<\/p>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131.png\" alt=\"Asp.NET MVC - Bir View&#039;da Tuple \u0130le \u00c7oklu Model Kullan\u0131m\u0131\" width=\"635\" height=\"138\" class=\"aligncenter size-full wp-image-3195\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131.png 635w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-300x65.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/a><\/p>\n<p>Yukar\u0131da yapt\u0131\u011f\u0131m\u0131z teknik sayesinde bir View&#8217;da birden fazla model kullanm\u0131\u015f olduk.Bu \u015fekilde bir kullan\u0131m teknik olarak m\u00fcmk\u00fcnd\u00fcr.Lakin proje ilerledik\u00e7e bu tekni\u011fin kullan\u0131m\u0131nda istemsiz s\u0131k\u0131nt\u0131lar do\u011fmakta ve yer yer Model i\u00e7indeki tan\u0131mlanan referansa g\u00f6nderilen de\u011ferler al\u0131namay\u0131p, null de\u011ferler d\u00f6nebilmektedir.\u0130\u015fte bu olas\u0131 durumu hi\u00e7 ya\u015famadan Tuple s\u0131n\u0131f\u0131yla daha profesyonel \u00e7oklu model kullan\u0131m\u0131n\u0131 g\u00f6relim.<\/p>\n<p>Modellerimizi ilk tan\u0131mlad\u0131\u011f\u0131m\u0131z haline getirdikten sonra View&#8217;\u0131 kulland\u0131\u011f\u0131m\u0131z Controller&#8217;da ki Action metotda a\u015fa\u011f\u0131daki i\u015flevi ger\u00e7ekle\u015ftirelim.&#8217;\u00d6rne\u011fimizde HomeController&#8217;da ki Index Action metodudur.&#8217;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        public ActionResult Index()\r\n        {\r\n            return View(Tuple.Create&lt;Okul, Ogretmen&gt;(new Okul(), new Ogretmen()));\r\n        }\r\n<\/pre>\n<p>Burada g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi Tuple s\u0131n\u0131f\u0131n\u0131n referans\u0131 \u00fczerinden Create metodu sayesinde \u00e7oklu model kullanaca\u011f\u0131m\u0131z View&#8217;a modellerimizi tan\u0131ml\u0131yoruz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n@using WebApplication14.Models\r\n\r\n@model Tuple&lt;Okul, Ogretmen&gt;\r\n\r\n@using (Html.BeginForm(&quot;Post&quot;, &quot;Home&quot;, FormMethod.Post))\r\n{\r\n    @:Ad\u0131:\r\n    @Html.TextBoxFor(model =&gt; model.Item2.Adi)\r\n    &lt;br \/&gt;\r\n        @:Soyad\u0131:\r\n        @Html.TextBoxFor(model =&gt; model.Item2.SoyAdi)\r\n        &lt;br \/&gt;\r\n        @:Maa\u015f\u0131:\r\n        @Html.TextBoxFor(model =&gt; model.Item2.Maasi)\r\n        &lt;br \/&gt;\r\n        @:Bran\u015f\u0131:\r\n        @Html.TextBoxFor(model =&gt; model.Item2.Bransi)\r\n        &lt;br \/&gt;\r\n        @:Okulu:\r\n        @Html.TextBoxFor(model =&gt; model.Item1.OkulAdi)\r\n\r\n        &lt;input type=&quot;submit&quot; value=&quot;Post Et&quot; \/&gt;\r\n}\r\n<\/pre>\n<p>View katman\u0131nda yukar\u0131daki \u00f6rnek kodda da g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi modelimizi Controller&#8217;da g\u00f6nderdi\u011fimiz nesne format\u0131nda, Tuple<Okul, Ogretmen> bi\u00e7iminde tan\u0131ml\u0131yoruz.<br \/>\nBu tan\u0131mlamadan sonra &#8220;Model&#8221; komutuna nokta(.) dedi\u011fimizde &#8220;Item1&#8221; \u00f6zelli\u011fi ilk parametredeki nesneyi, &#8220;Item2&#8221; \u00f6zelli\u011fi de ikinci parametredeki nesneyi sembolize etmektedir.Tuple s\u0131n\u0131f\u0131 ile 8 adet parametre tan\u0131mlayabiliriz.Bu demek oluyor ki, bir View&#8217;a 8 adet model g\u00f6nderebilme olana\u011f\u0131m\u0131z var demektir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n        &#x5B;HttpPost]\r\n        public ActionResult Post(&#x5B;Bind(Prefix = &quot;Item1&quot;)]Okul Model1, &#x5B;Bind(Prefix = &quot;Item2&quot;)]Ogretmen Model2)\r\n        {\r\n            return View();\r\n        }\r\n<\/pre>\n<p>Formun post i\u015flemi neticesinde HttpPost Attribute&#8217;u ile i\u015faretlenmi\u015f Action metoduna da yukar\u0131daki gibi parametre tan\u0131mlamas\u0131 yap\u0131yoruz.Burada post neticesinde Tuple s\u0131n\u0131f\u0131dan gelen parametleri, Action metodumuzdaki parametrelere Bind ediyoruz.&#8221;Prefix&#8221; \u00f6zelli\u011fi ile, i\u015faretlenen parametrenin Tuple&#8217;da ki hangi \u00f6zelli\u011fi sembolize edece\u011fini belirtiyoruz.Yukar\u0131daki \u00f6rne\u011fimizde &#8220;Item1&#8221; \u00f6zelli\u011fini &#8220;Okul&#8221; parametresinin temsil etmesini s\u00f6yl\u00fcyoruz.Bunun sebebi, Tuple s\u0131n\u0131f\u0131n\u0131 tan\u0131mlarken &#8220;Item1&#8221; \u00f6zelli\u011fine &#8220;Okul&#8221; nesnesini tan\u0131mlam\u0131\u015f olmam\u0131zd\u0131r.<\/p>\n<p>Bu teknikle yap\u0131land\u0131r\u0131lan post i\u015flemini Break &#8211; Point ile g\u00f6zlemlersek e\u011fer;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-2.png\" alt=\"Asp.NET MVC - Bir View&#039;da Tuple \u0130le \u00c7oklu Model Kullan\u0131m\u0131 - 2\" width=\"813\" height=\"92\" class=\"aligncenter size-full wp-image-3197\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-2.png 813w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-2-300x34.png 300w\" sizes=\"auto, (max-width: 813px) 100vw, 813px\" \/><\/a><br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-3.png\" alt=\"Asp.NET MVC - Bir View&#039;da Tuple \u0130le \u00c7oklu Model Kullan\u0131m\u0131 - 3\" width=\"1022\" height=\"131\" class=\"aligncenter size-full wp-image-3198\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-3.png 1022w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2015\/08\/Asp.NET-MVC-Bir-Viewda-Tuple-\u0130le-\u00c7oklu-Model-Kullan\u0131m\u0131-3-300x38.png 300w\" sizes=\"auto, (max-width: 1022px) 100vw, 1022px\" \/><\/a><\/p>\n<p>Ekran g\u00f6r\u00fcnt\u00fclerinden de g\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi iki nesnemizide bilgileri yakalam\u0131\u015f bir vaziyette elde etmi\u015f oluyoruz.Bu \u015fekilde bir kullan\u0131m olduk\u00e7a esneklik sa\u011flamakta ve profesyonel bir teknik edas\u0131 sergilemektedir.<\/p>\n<p>Son olarak, post an\u0131nda \u00e7al\u0131\u015fan Action metodda parametrelere Bind i\u015flemini g\u00f6z ard\u0131 ederseniz e\u011fer post i\u015fleminden sonra sayfan\u0131n kaynak kodlar\u0131yla kar\u015f\u0131la\u015fman\u0131z muhtemeldir.Buna dikkat ediniz \ud83d\ude09<\/p>\n<p>Bol bol faydalanman\u0131z dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Y\u00fcr\u00fcyorsan ko\u015fabilmelisin, ko\u015fuyorsan u\u00e7abilmelisin, u\u00e7uyorsan \u0131\u015f\u0131nlanabilmelisin.Durmamal\u0131s\u0131n.Bu g\u00fcn\u00fcn, d\u00fcn\u00fcnden bir ad\u0131m ileri olmal\u0131, tarihle yar\u0131\u015fmal\u0131, yar\u0131na daha \u00e7etin bir maratonla ba\u015flamal\u0131s\u0131n.Bu ya\u015fad\u0131\u011f\u0131m\u0131z \u00e7a\u011f\u0131n gereksinimlerindendir.\u00d6yleyse i\u015finde de \u00e7\u0131\u011f\u0131r a\u00e7mal\u0131s\u0131n.Evet, bu yaz\u0131m\u0131zda Asp.NET MVC&#8217;de (\u015fahsen)mimarisel mant\u0131\u011f\u0131 a\u015fmak&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,600],"tags":[745,746,738,739,740,737],"class_list":["post-3194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-mvc","category-asp-net-mvc-5","tag-asp-net","tag-asp-net-mvc","tag-asp-net-mvc-coklu-model","tag-asp-net-mvc-tuple-ile-coklu-model","tag-asp-net-mvc-viewda-birden-fazla-model-kullanimi","tag-tuple"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3194","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=3194"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/3194\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3014"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=3194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=3194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=3194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}