{"id":2505,"date":"2022-08-30T15:25:25","date_gmt":"2022-08-30T15:25:25","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/27\/is-this-a-way-to-call-the-model-dynamically-in-mvc-4-description-inside-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:25","modified_gmt":"2022-08-30T15:25:25","slug":"is-this-a-way-to-call-the-model-dynamically-in-mvc-4-description-inside-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/is-this-a-way-to-call-the-model-dynamically-in-mvc-4-description-inside-collection-of-common-programming-errors\/","title":{"rendered":"Is this a way to call the model dynamically in MVC 4? (Description inside)-Collection of common programming errors"},"content":{"rendered":"<p>When the Razor template gets compiled, <code>@(...)<\/code> expression is replaced by the call to <code>Write<\/code> inside the generated <code>Execute<\/code> method so your example would compile into the following:<\/p>\n<pre><code>public void Execute()\n{\n  \/\/ ...\n  dynamic r = base.Model.R;\n  Write((dynamic)base.Model.R);\n}\n<\/code><\/pre>\n<p>Since <code>Write<\/code> must accept a parameter of type <code>object<\/code>, anything can be passed (so I&#8217;m not sure you need a cast to <code>dynamic<\/code> here).<\/p>\n<p>Here&#8217;s an article that can help you understand how Razor templates are processed: Leveraging Razor Templates Outside of ASP.NET.<\/p>\n<p>As for <code>dynamic<\/code> keyword, this basically tells the compiler that the expression of this type will be <em>interpreted<\/em> at runtime (late binding). Here&#8217;s some more information: Using Type dynamic (MSDN).<\/p>\n<p>Hope this clarifies things a bit for you.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-27 08:40:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>When the Razor template gets compiled, @(&#8230;) expression is replaced by the call to Write inside the generated Execute method so your example would compile into the following: public void Execute() { \/\/ &#8230; dynamic r = base.Model.R; Write((dynamic)base.Model.R); } Since Write must accept a parameter of type object, anything can be passed (so I&#8217;m [&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-2505","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2505","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=2505"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2505\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}