{"id":2522,"date":"2022-08-30T15:25:34","date_gmt":"2022-08-30T15:25:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/28\/asp-net-mvc-inject-js-code-into-razor-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:34","modified_gmt":"2022-08-30T15:25:34","slug":"asp-net-mvc-inject-js-code-into-razor-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/asp-net-mvc-inject-js-code-into-razor-collection-of-common-programming-errors\/","title":{"rendered":"asp.net mvc inject js code into Razor-Collection of common programming errors"},"content":{"rendered":"<p>How I can do it?<\/p>\n<pre><code>$for (var i = 0; i &lt; model.CategoryList.length; i++) {\n   var text = model.CategoryList;\n   categories += '' + @((AgesCategory)int.Parse( ..... )).GetDescription() + '';\n }\n<\/code><\/pre>\n<p>I need replace <strong>int.Parse( &#8230;.. ))<\/strong> to <strong>int.Parse(model.CategoryList[i])<\/strong> from JS code. I can&#8217;t add <strong>text<\/strong> variable into <strong>int.Parse( &#8230; )<\/strong> intellisense detected this code like c# code, not JS.<\/p>\n<p><strong>update:<\/strong><\/p>\n<p>I&#8217;ll try reformulate my question: How I can set value C# variable from JS variable?<\/p>\n<pre><code>\n    var temp1 = 1;   \/\/ js var\n    @{ string temp2 = 0 ;}  \/\/ c# var\n    @{ temp2 = temp1 } \/\/ how do it?\n    var temp2 = @(((MyEnumType)int.Parse(temp2).GetEnumDescription()));  \/\/js var\n\n<\/code><\/pre>\n<p>what need use? &#8216;@:&#8217; or something else? In all case I get syntax error<\/p>\n<ol>\n<li>\n<p>I don&#8217;t think its possible. Because javascript execute at runtime, C# is compiled, it don&#8217;t know the values for a runtime javascript so will be error.<\/p>\n<p>You can have some study on &#8220;dynamic&#8221; types in C#, this is used in runtime.<\/p>\n<p>But sorry, I am not able to write a demo here because I haven&#8217;t much expirences on &#8220;dynamic&#8221; though I know it can do cross language things on C#. I can only give you this suggession.<\/p>\n<\/li>\n<li>\n<p>Understand that you can use @ expressions inside the blocks within your views. If you do so, some developer tools might not show code coloring or Intellisense for the JavaScript code block, but that won&#8217;t impair the ability of the site to compile and be run.<\/p>\n<p>HTH, Clay<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-28 10:03:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>How I can do it? $for (var i = 0; i &lt; model.CategoryList.length; i++) { var text = model.CategoryList; categories += &#8221; + @((AgesCategory)int.Parse( &#8230;.. )).GetDescription() + &#8221;; } I need replace int.Parse( &#8230;.. )) to int.Parse(model.CategoryList[i]) from JS code. I can&#8217;t add text variable into int.Parse( &#8230; ) intellisense detected this code like c# [&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-2522","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2522","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=2522"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2522\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}