asp.net mvc inject js code into Razor-Collection of common programming errors

How I can do it?

$for (var i = 0; i < model.CategoryList.length; i++) {
   var text = model.CategoryList;
   categories += '' + @((AgesCategory)int.Parse( ..... )).GetDescription() + '';
 }

I need replace int.Parse( ….. )) to int.Parse(model.CategoryList[i]) from JS code. I can’t add text variable into int.Parse( … ) intellisense detected this code like c# code, not JS.

update:

I’ll try reformulate my question: How I can set value C# variable from JS variable?


    var temp1 = 1;   // js var
    @{ string temp2 = 0 ;}  // c# var
    @{ temp2 = temp1 } // how do it?
    var temp2 = @(((MyEnumType)int.Parse(temp2).GetEnumDescription()));  //js var

what need use? ‘@:’ or something else? In all case I get syntax error

  1. I don’t think its possible. Because javascript execute at runtime, C# is compiled, it don’t know the values for a runtime javascript so will be error.

    You can have some study on “dynamic” types in C#, this is used in runtime.

    But sorry, I am not able to write a demo here because I haven’t much expirences on “dynamic” though I know it can do cross language things on C#. I can only give you this suggession.

  2. 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’t impair the ability of the site to compile and be run.

    HTH, Clay