Googlemap control for Asp.Net MVC

Binding To Model

About this example

This example shows how to databind Googlemap Control for ASP.NET MVC.

The required steps are:

  1. Pass an IEnumerable<T> to the view:

    public ActionResult DataBindingToModel()
    {
        IEnumerable<MarkerData> data = DataContext.GetRegions();
        return View(data);
    }
        
  2. Pass the collection as the first parameter of the BindTo method. The second parameter is an Action<MapObjectBindingFactory<Marker>> which is used to define mappings between objects and Google Markers.
    The For<T> method is used to configure the binding. The ItemDataBound method maps properties of T to Marker.

    Here is a Googlemap declaration showing how to bind the component to IEnumerable<Marker>

    @( Html.Googlemap()
            .Name("map")
            .BindTo<DataContext.MarkerData, Marker>
            (Model, mappings => 
            {
                mappings.For<DataContext.MarkerData>(binding => binding
                        .ItemDataBound(marker, obj) =>
                        {
                            marker.Latitude = obj.Latitude;
                            marker.Longitude = obj.Longitude;
                            marker.Title = obj.Title;
                            marker.zIndex = obj.zIndex;
                        })
                );
            })
    )