{"id":313,"date":"2021-02-16T20:06:00","date_gmt":"2021-02-16T23:06:00","guid":{"rendered":"http:\/\/fabiobrandao.net.br\/blog\/?p=313"},"modified":"2023-03-12T18:45:13","modified_gmt":"2023-03-12T21:45:13","slug":"como-preencher-um-dropdownlist-com-dados-da-viewbag","status":"publish","type":"post","link":"https:\/\/fabiobrandao.net.br\/blog\/asp-net-mvc\/como-preencher-um-dropdownlist-com-dados-da-viewbag\/","title":{"rendered":"Como preencher um DropdownList com dados da ViewBag"},"content":{"rendered":"<p>Para preencher um DropDownList com dados da <strong>ViewBag<\/strong> utilizando <strong>ASP.NET MVC<\/strong> \u00e9 muito simples. Primeiro voc\u00ea precisa implementar o ViewBag do lado do controlador com os dados que ser\u00e3o apresentados. Veja um exemplo:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nViewBag.Estados = ObterEstados();\r\n<\/pre>\n<p>No exemplo acima a ViewBag Estados receber\u00e1 um IEnumerable com dados dos estados que \u00e9 composto pelos atributos Id e Sigla.<\/p>\n<p>Agora em sua View voc\u00ea deve implementar o seu <strong>DropDownList<\/strong> como no exemplo abaixo:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@Html.DropDownList(&quot;Estados&quot;, \r\nnew SelectList((System.Collections.IEnumerable)ViewBag.Estados, \r\n   &quot;Id&quot;, &quot;Sigla&quot;), \r\n   &quot;Selecione uma op\u00e7\u00e3o&quot;, \r\n   new { @value = &quot;&quot; })\r\n<\/pre>\n<p>M\u00e3o na massa, um abra\u00e7o!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Para preencher um DropDownList com dados da ViewBag utilizando ASP.NET MVC \u00e9 muito simples. Primeiro voc\u00ea precisa implementar o ViewBag do lado do controlador com os dados que ser\u00e3o apresentados&#8230;. <\/p>\n","protected":false},"author":1,"featured_media":397,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[11],"tags":[64,16],"class_list":["post-313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-asp-net-mvc","tag-asp-net","tag-dropdown"],"_links":{"self":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":6,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/posts\/313\/revisions\/319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/media\/397"}],"wp:attachment":[{"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fabiobrandao.net.br\/blog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}