{"id":6546,"date":"2014-04-20T08:20:58","date_gmt":"2014-04-20T08:20:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/problem-about-viewbag-collection-of-common-programming-errors\/"},"modified":"2014-04-20T08:20:58","modified_gmt":"2014-04-20T08:20:58","slug":"problem-about-viewbag-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/problem-about-viewbag-collection-of-common-programming-errors\/","title":{"rendered":"problem about viewbag-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/Y5Fp1.jpg?s=32&amp;g=1\" \/><br \/>\nmattytommo<br \/>\nc# asp.net-mvc performance viewbag<br \/>\nWhen I think of a ViewBag, I associate it with the &#8220;final say.&#8221;Meaning you do all your logic and set the results to it.ViewBag.A = Some crazy math formula; ViewBag.B = Another crazy math formula; ViewBag.GrandTotal = Some crazy math formula + Another crazy math formula;This works as well:ViewBag.A = Some crazy math formula; ViewBag.B = Another crazy math formula; ViewBag.GrandTotal = ViewBag.A + ViewBag.B;Yipes, I am working with variables that formulate at runtime!Is this in anyway absurd or in<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/40de750233b3ce8268f235ba0b6a489b?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nteresko<br \/>\nasp.net-mvc razor viewbag<br \/>\nFor the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model?Question:Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view model) Are there situations where a ViewBag is absolutely necessary?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/ae15c48f686a0ecb39848f980b296611?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nShog9<br \/>\nc# asp.net-mvc dynamic viewbag<br \/>\nI am reading a book on ASP.NET MVC and I&#8217;m wondering how the following example works:Example #1Controllerpublic class MyController : Controller {public ActionResult Index(){ViewBag.MyProperty = 5;return View();} }View&lt;h1&gt;@ViewBag.MyProperty&lt;\/h1&gt;Now I understand that ViewBag is a dynamic object, so that&#8217;s how you can set the property (though I don&#8217;t know much about dynamic objects, never worked with them.) But how does the view get the specific instance of the ViewBag from the control<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c35af79e54306caedad37141f13de30c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nH.B.<br \/>\nc# dynamic .net-4.0 asp.net-4.0 viewbag<br \/>\nI have a situation where I would like to do something simular to what was done with the ASP.NET MVC 3 ViewBag object where properties are created at runtime? Or is it at compile time?Anyway I was wondering how to go about creating an object with this behaviour?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/11fedd06ab2a8a6626927674f4dfb7dd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSteve Newton<br \/>\nlinq asp.net-mvc-3 viewbag<br \/>\nDoes anyone have the faintest idea what this error means please and how to resolve it? All my research is drawing a blank, I can see how to set it on MSDN but it doesn&#8217;t explain it in a way that explains to me what the issue is. If I remove some of my LINQ queries to set viewbag items then it seems to resolve it but the moment I set new ones and pass them into my view to generate a mail for MVCMailer it comes back. Not sure if its a viewbag issue or simply that I am calling too many linq quer<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/553d02417440b7dd2467844a2cdeda88?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nSaeed Neamati<br \/>\nasp.net-mvc-3 dynamic razor null viewbag<br \/>\nConsider this simple controller:Porduct product = new Product(){\/\/ Creating a product object; }; try {productManager.SaveProduct(product);return RedirectToAction(&#8220;List&#8221;); } catch (Exception ex) {ViewBag.ErrorMessage = ex.Message;return View(&#8220;Create&#8221;, product); }Now, in my Create view, I want to check ViewBag object, to see if it has Error property or not. If it has the error property, I need to inject some JavaScript into the page, to show the error message to my user.I created an extension meth<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0ec5316b8c8703f98e6e369501c86717?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAmanda<br \/>\njavascript mvc viewbag<br \/>\nI would like to access ViewBag data from within client side code. This is what I&#8217;ve tried in my controller:ViewBag.Tasks = new JavaScriptSerializer().Serialize(tasks);In my view I would then put this in a hidden field so that it available client side:&lt;input type=&#8221;hidden&#8221; name=&#8221;diagramData&#8221; data-nodes=&#8221;@ViewBag.Tasks&#8221; \/&gt;In my Javascript file, I would then search for the info related to the selected node:alert($(&#8220;#diagramData&#8221;).data(&#8220;nodes&#8221;)); \/\/?????The alert always shows &#8220;undefined&#8221;. W<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/048442c95fe80c87a7a6fada8cc852fc?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nCaKaL<br \/>\nasp.net asp.net-mvc-3 viewbag<br \/>\nvar Urun = (from u in db.urunlers.Where(x =&gt; x.Yayinlama == &#8220;1&#8221;)where u.UrunID == idselect new{u.UrunID,UrunAdi= u.UrunAdi,u.UrunAciklama,u.YorumIzni}).SingleOrDefault();ViewBag.urun = Urun;This is codebehind in my &#8220;.cs&#8221; page.var Urun = ViewBag.urun; &lt;h3 class=&#8221;Baslik&#8221;&gt;&lt;%:Urun.UrunAdi %&gt;&lt;\/h3&gt;This is my code &#8220;.aspx&#8221; page.But i get &#8216;object&#8217; does not contain a definition for &#8216;UrunAdi&#8217; error?I solved my problem via another method:JavaScriptSerializer js = new JavaScriptSerialize<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mattytommo c# asp.net-mvc performance viewbag When I think of a ViewBag, I associate it with the &#8220;final say.&#8221;Meaning you do all your logic and set the results to it.ViewBag.A = Some crazy math formula; ViewBag.B = Another crazy math formula; ViewBag.GrandTotal = Some crazy math formula + Another crazy math formula;This works as well:ViewBag.A = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6546","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6546"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6546\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}