# Bind Map to View Model

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Map for Telerik UI for ASP.NET MVC</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

## Description

How can I bind the ASP.NET MVC Map component to a Model collection?

## Solution

MVC enables you to natively bind parts of the page to the Model that is passed from the controller.

Using this technique, you can also bind Telerik UI for ASP.NET MVC Map options to the Model properties and populate the Map markers based on a collection that is defined in the Model.

1. Set up the Models.

```C# Map
public class Map
{
    // The Map properties.
    public string Name { get; set; }
    public double CenterLatitude { get; set; }
    public double CenterLongitude { get; set; }
    public int Zoom { get; set; }

    // The tile layer properties.
    public string TileUrlTemplate { get; set; }
    public string[] TileSubdomains { get; set; }
    public string TileAttribution { get; set; }

    // The markers collection.
    public IEnumerable<Marker> Markers { get; set; }
}
```

```C# Marker
public class Marker
{
    public Marker(double latitude, double longitude, string sname)
    {
        latlng = new double[] { latitude, longitude };
        name = sname;
    }

    public double[] latlng { get; set; }
    public string name { get; set; }
}
```

2. Set up the desired data in the Controller and send it to the View.

```C# Controller
public ActionResult Index()
{
    var map = new TelerikMvcApp.Models.Map()
    {
        Name = "MyMap",
        CenterLatitude = 30.268107,
        CenterLongitude = -97.744821,
        Zoom = 3,
        TileUrlTemplate = "http://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png",
        TileSubdomains = new string[] { "a", "b", "c" },
        TileAttribution = "\u00A9 OpenStreetMap contributors",
        Markers = new List<Marker>
        {
            new Marker(30.268107, -97.744821, "Austin, TX")
        }
    };

    return View(map);
}
```

3. Configure the Map using the Model properties accessible in the View.

```Razor HtmlHelper
@model TelerikMvcApp.Models.Map

@(Html.Kendo().Map()
        .Name(Model.Name)
        .Center(Model.CenterLatitude, Model.CenterLongitude)
        .Zoom(Model.Zoom)
        .Layers(layers => layers
            .Add()
            .Type(MapLayerType.Tile)
            .UrlTemplate(Model.TileUrlTemplate)
            .Subdomains(Model.TileSubdomains)
            .Attribution(Model.TileAttribution)
        )
        .Markers(markers =>
        {
            foreach (var marker in Model.Markers)
            {
                markers.Add()
                .Location(marker.latlng)
                .Title(marker.name);
            }
        })
)
```

## More ASP.NET MVC Map Resources

* [ASP.NET MVC Map Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/diagrams-and-maps/map/overview)

* [ASP.NET MVC Map Demos](https://demos.telerik.com/aspnet-mvc/map/index)

* [ASP.NET MVC Map Product Page](https://www.telerik.com/aspnet-mvc/map)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Basic Usage of the Map HtmlHelper for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/map)

* [Server-Side API Reference of the Map for ASP.NET MVC](https://www.telerik.com/aspnet-mvc/documentation/api/map)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
