当我想在调试按钮上启动我的应用程序时,我得到了一个错误消息:传入字典的模型项类型为'System.Collections.Generic.List ' 1[Something.Models. list]。,但是这个字典需要一个类型为'Something.Models.GoogleMapModel'的模型项。我不明白为什么会这样。我在Stackowerflow上找不到解决方案,所以任何帮助都会很好。
这是我的模型:
namespace Something.Models
{
public class GoogleMapModel
{
public string Title { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
}
}
控制器:
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>
将视图绑定到列表:
@model List<Something.Models.GoogleMapModel>
变化
@model Something.Models.GoogleMapModel
@model List<Something.Models.GoogleMapModel>
在MapView