On my homecontroller's index view page, I want to display data extracted from database on a Google map. So, what I do is include the model.cs file in homecontroller.cs file and pass the entire database data to view by converting it to list and passing as a Viewbag object. But now when I try to display markers on the map based on database's data, it doesn't show. Even a simple javascript alert() statement doesn't show anything on screen. Following is the code:-
-----------------EventsRegister.cs(Model page)------------
namespace CampusConnect.Models
{
public class EventReg
{
public int ID { get; set; }
public string UniqueId { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
public string Description { get; set; }
}
public class EventDBContext: DbContext
{
public DbSet<EventReg> Events { get; set; }
}
}
-----------------Homecontroller.cs-----------------------
namespace CampusConnect.Controllers
{
public class HomeController : Controller
{
private EventDBContext db = new EventDBContext();
private List<EventReg> list;
public ActionResult Index()
{
list = db.Events.ToList();
ViewBag.Message = "Please Register/Login to continue";
ViewBag.EntireList= list;
return View();
}
}
}
----------------Index.cshtml(Homecontroller's index view)----------------
@section bodyOrMap {
@if (Request.IsAuthenticated)
{
<text>
<div class="content-wrapper">
@*Google Maps included*@
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyC7TFDjCbI4A10H2J-f6zBFHToFQIs6Z2M&sensor=true"></script>
<script type="text/javascript">
function InitializeMap() {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(29.8644, 77.8964);
var myOptions = {
disableDoubleClickZoom: true,
zoom: 17,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
@foreach (var item in ViewBag.EntireList)
{
<text>
alert(@item.Description); -------->THIS DOESN'T PRINT
</text>
}
var aListener = google.maps.event.addListener(map, 'dblclick', function (event) {
alert("Double click displays values" + event.latLng.lat() + " " + event.latLng.lng());
});
} //Initialise map function ends
window.onload = InitializeMap;
</script>
<div id="map" style="top: 0px; left: 0px; /*width: 500px;*/ height: 338px; position: relative; float: left; display: inline; width:49%">
</div>
</asp:Content>
</div>
</text>
}
else
{
//some HTML
}
}
The map gets displayed but the alert statement doesn't print. Also, the double click event handler doesn't work(Although, it worked before when no data was there in the database). I have checked the database and there is a table Events.mdf with a row of data that I entered.
Can somebody tell me what the problem is? I am a newbie at programming in asp.net so it might be a minor point. Thanks.