Home > Web Front-end > JS Tutorial > How Can I Embed Razor Syntax within JavaScript in ASP.NET MVC Views?

How Can I Embed Razor Syntax within JavaScript in ASP.NET MVC Views?

Susan Sarandon
Release: 2024-12-04 10:07:12
Original
642 people have browsed it

How Can I Embed Razor Syntax within JavaScript in ASP.NET MVC Views?

Using Razor Syntax within JavaScript in Views

Despite Razor's limitation of generating HTML, the pseudo-element can force it into content mode, allowing JavaScript to be included in views.

<script type="text/javascript">

    // Some JavaScript code here to display map, etc.

    // Now add markers
    @foreach (var item in Model) {
        <text>
            var markerlatLng = new google.maps.LatLng(@(Model.Latitude), @(Model.Longitude));
            // ... JavaScript code to add markers
        </text>
    }
</script>
Copy after login

An alternative to is the @: syntax, which switches Razor into text mode.

<script type="text/javascript">

    // Declare addMarker function
    function addMarker(latitude, longitude, title, description, map)
    {
        // ... JavaScript code
    }

    // Now add markers
    @foreach (var item in Model) {
Copy after login

The above is the detailed content of How Can I Embed Razor Syntax within JavaScript in ASP.NET MVC Views?. 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