简体   繁体   English

将值从 Javascript 发送到 Blazor 不起作用(双数据绑定)

[英]Sending values from Javascript to Blazor doesn't work (Two-data-binding)

In my java script I'm sending the Lan/Long of my map to blazor. I can see the the the longitude and latitude changes on the browser, but I want to pass them to my blazor method to do some processing.在我的 java 脚本中,我将我的 map 的 Lan/Long 发送到 blazor。我可以在浏览器上看到经度和纬度的变化,但我想将它们传递给我的 blazor 方法进行一些处理。 the values returned always 0.返回的值始终为 0。

Here is my have script method:这是我的脚本方法:

function GetCircle(e){
    r = document.getElementById("InputRadius").value;

    if (circle !== null) {
        map.removeLayer(circle);

    }
        circle = new  L.circle([ Lat,Long],{radius:r});  
    map.addLayer(circle);
    circle.bindPopup( "<br/> Radius is : " + r +" Meter" 
   
    ).openPopup();
    document.getElementById("Lat").value = Lat; //send this to blazor
    document.getElementById("Long").value = Long; // send this to blazor
 
}

As you can see from the above code, when the user click on the map I'm getting the long/lat and then send them to blazor.从上面的代码可以看出,当用户点击 map 时,我得到了 long/lat,然后将它们发送到 blazor。

My razor page: I have the following inputs which I'm using them to store the data inside them then pass the data to blazor method in the 'code' section我的 razor 页面:我有以下输入,我正在使用它们将数据存储在其中,然后将数据传递给“代码”部分中的 blazor 方法


<input type="text" @bind="Lat" id="Lat" name="Lat"   > 
<input type="text" @bind="Long" id="Long" name = "Long" >

@code {
   
   int InputRadius;
  double Long;
  double Lat; 
  string date_selected;
  string time_selected;
  


    public async Task GetTgas()
        {
         AllTags = await Task.Run(() => tagsService.Map(   InputRadius, Long,Lat,date_selected,time_selected));
 
        }

 public async Task GetCircle()

    {
        await JSRuntime.InvokeVoidAsync("GetCircle");

    }

}

I can see the Lan/Long in my view perfectly when I'm clicking on GetCircle, which means that they passed from JS to blazor page successfully, but when doing the GetTags function they don't pass to my blazor method.当我点击 GetCircle 时,我可以在我的视图中完美地看到 Lan/Long,这意味着它们成功地从 JS 传递到 blazor 页面,但是在执行 GetTags function 时,它们不会传递到我的 blazor 方法。

Any help would be appriciated!任何帮助将不胜感激!

Your GetCircle method should be like this, need to send .net object reference to javascript function你的GetCircle方法应该是这样的,需要发送.net object参考javascript function

await JS.InvokeVoidAsync("GetCircle", DotNetObjectReference.Create(this));

And your GetTags function should be like this, should be invokable from javascript你的 GetTags function 应该是这样的,应该可以从 javascript 调用

[JSInvokable]
public async Task GetTags(double lati, double longi)
   {
      AllTags = await Task.Run(() => tagsService.Map(InputRadius, longi,lati,date_selected,time_selected));     
   }

Your javascript function should be like this你的 javascript function 应该是这样的

function GetCircle(dotNetHelper){
    r = document.getElementById("InputRadius").value;

    if (circle !== null) {
        map.removeLayer(circle);

    }
        circle = new  L.circle([ Lat,Long],{radius:r});  
    map.addLayer(circle);
    circle.bindPopup( "<br/> Radius is : " + r +" Meter" 
   
    ).openPopup();

    //  send this to blazor
    dotNetHelper.invokeMethodAsync('GetTags', Lat, Long);     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM