I'm pretty confused with how to mix razor and js. This is the current function I am stuck with:
<script type="text/javascript">
var data = [];
@foreach (var r in Model.rows)
{
data.push([ @r.UnixTime * 1000, @r.Value ]);
}
If I could declare c# code with <c#></c#>
and everything else was JS code -- this would be what I am after:
<script type="text/javascript">
var data = [];
<c#>@foreach (var r in Model.rows) {</c#>
data.push([ <c#>@r.UnixTime</c#> * 1000, <c#>@r.Value</c#> ]);
<c#>}</c#>
What is the best method to achieve this?
Use <text>
:
<script type="text/javascript">
var data = [];
@foreach (var r in Model.rows)
{
<text>
data.push([ @r.UnixTime * 1000, @r.Value ]);
</text>
}
</script>
Inside a code block (eg, @foreach
), you need to mark the markup (or, in this case, Javascript) with @:
or the <text>
tag.
Inside the markup contexts, you need to surround code with code blocks (@{ ... }
or @if
, ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With