Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
Call JavaScript functions from .NET methods in ASP.NET Core Blazor

JavaScript function that I want to call from my ASP.NET Core Blazor app:

export function onClick(map) {
    var latLng = null;
    function onMapClick(e) {
        latLng = map.unproject(e.point).toString();
        alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
    }
    map.on('click', onMapClick);
    alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
    return String(yourGlobalVariable);
}

C# code where I'm calling that function:

        string res = await mapModule.InvokeAsync<string>("onClick", mapInstance);

Where mapInstance and mapModule are IJSObjectReference

The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?

I've tried similar code here: http://jsfiddle.net/mufjkv6r/1/ and it works

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?

First thing I want to say is Jsfiddle contain normal flow of execution, which is executing one line after another.

 var latLng = null;
    function onMapClick(e) {
        latLng = map.unproject(e.point).toString();
        alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
    }
    map.on('click', onMapClick);
    alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
    return String(yourGlobalVariable);

but the above code will not call onMapClick function unless you clicked on the map. So the latLng will be null at that point of time.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda