The value of "ShapeSelectedEventArgs Data" is null when the ShapeSelected event is executed

This is the code : 


<h1>Hello, Syncfusion Playground for Blazor!</h1>

@using Syncfusion.Blazor.Maps


<SfMaps>
    <MapsEvents ShapeSelected="@onShapeSelected"></MapsEvents>
    <MapsLayers>
        <MapsLayer TValue="DatosMapa" DataSource="@_mapa.DatosMapas" ShapeDataPath="Continent" ShapePropertyPath=@(new string[] {"continent"}) ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}'>
            <MapsLayerSelectionSettings Enable="true" Fill="#000000" />
        </MapsLayer>
    </MapsLayers>
</SfMaps>

@code {
    public string shapePropertyPath = "continent";
    public string shapeDataPath = "Continent";
    public class DatosMapa
    {
        public string Admin;
        public string Name;
        public string Continent;
    };
    public Mapa _mapa { get; set; }
    protected override async Task OnInitializedAsync()
    {
        _mapa = new Mapa();
        _mapa.DatosMapas = DataSource;
    }

    public class Mapa
    {
        public List<DatosMapa> DatosMapas { get; set; }
    }
    public List<DatosMapa> DataSource = new List<DatosMapa>
{
        new DatosMapa { Admin = "Sales", Continent= "North America", Name="USA"},
        new DatosMapa { Admin = "Manufacture", Continent = "South America", Name = "US"},
        new DatosMapa { Admin = "Quality", Continent= "Africa", Name="Africa"},
        new DatosMapa { Admin = "Marketing", Continent = "Europe", Name = "Euro"}
    };


    void onShapeSelected(ShapeSelectedEventArgs args)
    {
        DatosMapa temp = Newtonsoft.Json.JsonConvert.DeserializeObject<DatosMapa>(args.Data.ToString());
        Console.WriteLine(temp.Continent);
    }
}


Thanks


3 Replies

HP Hemanathan Pandian Syncfusion Team November 13, 2023 03:38 PM UTC

Hi,


When we analyzed the provided code snippet, we noticed that the class “DatosMapa” does not have the members as properties. We need to provide get and set methods to the properties in the class to return proper values from the “Data” property in the event argument of “ShapeSelected” event. The “Data” property holds the value from the data source for the corresponding shape. If the shape is not bound with the data source, it will return “null” value. When the “null” value is returned, you can use “ShapeData” property which will return the properties object from the GeoJSON data. With the provided code, when clicking on the “Asia” shape return “null” value in the “Data” property as “Asia” shape is not bound to the data source.


Code Snippet:

@using Syncfusion.Blazor.Maps

 

<SfMaps>

    <MapsEvents ShapeSelected="@onShapeSelected"></MapsEvents>

    <MapsLayers>

        <MapsLayer TValue="DatosMapa" DataSource="@_mapa.DatosMapas" ShapeDataPath="Continent" ShapePropertyPath=@(new string[] {"continent"}) ShapeData='new {dataOptions= https://cdn.syncfusion.com/maps/map-data/world-map.json}'>

            <MapsLayerSelectionSettings Enable="true" Fill="#000000" />

        </MapsLayer>

    </MapsLayers>

</SfMaps>

 

@code {

    public string shapePropertyPath = "continent";

    public string shapeDataPath = "Continent";

    public class DatosMapa

    {

        public string Admin { get; set; }

        public string Name { get; set; }

        public string Continent { get; set; }

    };

    public Mapa _mapa { get; set; }

    protected override async Task OnInitializedAsync()

    {

        _mapa = new Mapa();

        _mapa.DatosMapas = DataSource;

    }

 

    public class Mapa

    {

        public List<DatosMapa> DatosMapas { get; set; }

    }

    public List<DatosMapa> DataSource = new List<DatosMapa>

{

        new DatosMapa { Admin = "Sales", Continent= "North America", Name="USA"},

        new DatosMapa { Admin = "Manufacture", Continent = "South America", Name = "US"},

        new DatosMapa { Admin = "Quality", Continent= "Africa", Name="Africa"},

        new DatosMapa { Admin = "Marketing", Continent = "Europe", Name = "Euro"}

    };

 

 

    void onShapeSelected(ShapeSelectedEventArgs args)

    {

        If (args.Data != null) {

        DatosMapa temp = Newtonsoft.Json.JsonConvert.DeserializeObject<DatosMapa>(args.Data.ToString());

        Console.WriteLine(temp.Continent);

    }

    }

}


Please let us know if you need any further assistance.



MA Manuel replied to Hemanathan Pandian November 13, 2023 04:54 PM UTC

thanks Hemanathan,


it works like a charm!!


Image_8943_1699893040325



IR Indumathi Ravi Syncfusion Team November 14, 2023 09:42 AM UTC

Hi Manuel,


Thank you for the update.


Please get back to us if you need any further assistance.


Loader.
Up arrow icon