Home > Web Front-end > JS Tutorial > How to Cleanly Mix Razor and JavaScript Code in ASP.NET?

How to Cleanly Mix Razor and JavaScript Code in ASP.NET?

Barbara Streisand
Release: 2024-11-02 17:29:02
Original
607 people have browsed it

How to Cleanly Mix Razor and JavaScript Code in ASP.NET?

Mixing Razor and JavaScript Code

Mixing Razor and JavaScript code can be confusing at first. Consider the following function:

<script type="text/javascript">
        var data = [];

        @foreach (var r in Model.rows)
        {
                data.push([ @r.UnixTime * 1000, @r.Value ]);
        }
</script>
Copy after login

It would be much cleaner to declare C# code using and tags, like this:

<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#>
</script>
Copy after login

To achieve this, use the tag:

<script type="text/javascript">
   var data = [];

   @foreach (var r in Model.rows)
   {
      <text>
            data.push([ @r.UnixTime * 1000, @r.Value ]);
      </text>
   }
</script>
Copy after login

The above is the detailed content of How to Cleanly Mix Razor and JavaScript Code in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template