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
Susan SarandonOriginal
2024-12-04 10:07:12653browse

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>

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) {

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!

Statement:
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