locked
ASP.NET JavaScript in a C# block syntax RRS feed

  • Question

  • User-217557848 posted

    Hello,

    Consider the following. I can include a block of JavaScript in my views like this:

    <script type="text/javascript">
    </script>

    Then I can do something with embedded C# like this:

    <script type="text/javascript">
        @foreach (var value in new[] {1, 2, 3})
        {
            // Do something with the value....
        }
    </script>

    Now, consider, I want to embed some JavaScript, in this case with a jQuery, within the body of the C# loop. It is possible in a single line, for starters.

    <script type="text/javascript">
        @foreach (var value in new[] {1, 2, 3})
        {
            @:$("<p>").html("@(value)");
        }
    </script>

    But what do I do for a more complex, multiline "@:"?

    For anything more complex than that, having only a single line of "@:" syntax is REALLY cumbersome.

    Monday, May 2, 2016 2:45 PM

Answers

  • User-217557848 posted

    I decided to translate the C# details into a JavaScript array of objects and work with that through a jQuery handler. Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 2, 2016 3:38 PM

All replies

  • User753101303 posted

    Hi,

    Rather than going back and forth between js and C# you could also consider to pass data from C# to JavaScript and use JavaScript. Also you could use a C# helper maybe rather than doing that in the view.

    Some more context might help to figure out how one would handle that but going back and forth numerous times between C# and Js would be likely one of my very last choice.

    With your actual scenario can't you pass your data to js and be done with that?

    Monday, May 2, 2016 3:04 PM
  • User-217557848 posted

    PatriceSc

    Rather than going back and forth between js and C# you could also consider to pass data from C# to JavaScript and use JavaScript. Also you could use a C# helper maybe rather than doing that in the view.

    So, no multiline "@:" syntax?

    There are helpers for tables? Rows? Cells?

    Monday, May 2, 2016 3:19 PM
  • User-217557848 posted

    I decided to translate the C# details into a JavaScript array of objects and work with that through a jQuery handler. Thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 2, 2016 3:38 PM
  • User753101303 posted

    Ah, sorry, but for some reason it seems I missed the actual question was less general than that. This is a quite short script block? Try perhaps http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ and search for "Mixing code and Plain text". The "text" tag might be another to solve your issue.

    Monday, May 2, 2016 3:55 PM
  • User-1327394822 posted

    for multiline in razor you can use the <text> tag:

    eg:

    <text>

    write any number of 

    lines of code

    here...

    </text>

    Monday, May 2, 2016 4:51 PM