0

I got an error message when I want to start my app on debug button: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Something.Models.GoogleMapModel]', but this dictionary requires a model item of type 'Something.Models.GoogleMapModel'. I cannot figure out why this is happening. I can't find solution on Stackowerflow so any help will be good.

This is my Model:

namespace Something.Models
{
    public class GoogleMapModel
    {
        public string Title { get; set; }

        public double Lat { get; set; }

        public double Lng { get; set; }
    }
}

Controller:

namespace Something.Controllers
{
    public class GoogleMapController : Controller
    {
        [HttpGet]
        public ActionResult MapView()
        {
            var cities = new List<GoogleMapModel>();
            cities.Add(new GoogleMapModel() { Title = "Place", Lat = 48.4516, Lng = 13.6271 });
            return View(cities);
        }
    }
}

MapView:

@model Something.Models.GoogleMapModel

@{
    ViewBag.Title = "MapView";
}
    <script>
var cities = @Html.Raw(Json.Encode(Model));
//console.log(cities);

$(document).ready(function() {
    // execute
    (function() {
        // map options
        var options = {
            zoom: 6,
            center: new google.maps.LatLng(45.1000, 15.2000),
            mapTypeId: google.maps.MapTypeId.TERRAIN,
            mapTypeControl: false
        };

        // init map
        var map = new google.maps.Map(document.getElementById('map_canvas'), options);

        for (var i = 0; i < cities.length; i++) {
            // init markers
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(cities[i].Lat, cities[i].Lng),
                map: map,
                title: cities[i].Title
            });

            // process multiple info windows
            (function(marker, i) {
                // add click event
                google.maps.event.addListener(marker, 'click', function() {
                    infowindow = new google.maps.InfoWindow({
                        content: cities[i].Title
                    });
                    infowindow.open(map, marker);
                });
            })(marker, i);
        }
    })();
});

    </script>
Mima Mima
  • 17
  • 5

2 Answers2

1

Bind the view to a list:

@model List<Something.Models.GoogleMapModel>
GH DevOps
  • 213
  • 1
  • 10
  • Thank you. And I have one more question, Can I have included two models on the same view and if I can how ? – Mima Mima Aug 04 '21 at 12:20
  • 1
    No, you'll have to combine both models into one (view model) – GH DevOps Aug 04 '21 at 12:26
  • @MimaMima [this](https://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3) and [this](https://stackoverflow.com/questions/4764011/multiple-models-in-a-view) may help – Harshad Raval Aug 04 '21 at 13:10
1

Change

@model Something.Models.GoogleMapModel

to

@model List<Something.Models.GoogleMapModel>

in MapView

Harshad Raval
  • 73
  • 2
  • 9