Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!