I'm developing a website that should display and respond nicely on both desktop and mobile browsers. Hence, I'm using the nice features of MVC 4 to switch between views using the .mobile.cshtml extensions. Since I'm familiar with jQuery I would like to use jQuery Mobile for the mobile views.
So, I've created a _Layout.Mobile.cshtml view with standard html, header and body elements. The body element contains the following:
<body>
<div data-role="page" id="@ViewBag.PageId">
<div data-role="header" data-position="fixed">
<h1>@ViewBag.Title</h1>
@Html.ActionLink("Indstillinger", "Manage", "Account", null, new { @class= "ui-btn-right", data_icon = "gear"})
<div data-role="navbar">
@RenderSection("PageNavigation", required: false)
</div>
</div>
<div data-role="content" class="map-content">
@RenderBody()
</div>
</div><!-- /page -->
</body>
The PageNavigation section is used to place a jQuery Mobile persistent navbar that contains two items pointing to two different views.
The first view looks like this:
@model MyModel
@{
ViewBag.Title = "Some title";
ViewBag.PageId = "pageuniquename";
Layout = "~/Views/Shared/_Layout.Mobile.cshtml";
}
@section PageNavigation
{
<ul>
<li>@Html.ActionLink("List display", "Index", "Controller",new { @class= "ui-btn-active ui-state-persist"})</li>
<li>@Html.ActionLink("Map display", "Map", "Controller")</li>
</ul>
}
@Html.Partial("~/Views/Shared/Mobile/_TestPartial.cshtml", Model)
The second view looks like this:
@model MyModel
@{
ViewBag.Title = "Map display";
ViewBag.PageId = "mappage";
Layout = "~/Views/Shared/_Layout.Mobile.cshtml";
}
@section PageNavigation
{
<ul>
<li>@Html.ActionLink("List display", "Index", "Controller")</li>
<li>@Html.ActionLink("Map display", "Map", "Controller",new { @class= "ui-btn-active ui-state-persist"})</li>
</ul>
}
<div id="mapCanvas"></div>
The Google map is initialized by this code in the layout page:
$(document).on("pageshow", "#mappage", initializeMap);
The map initializes fine and I'm able to add markers. I can switch between the pages through the navbar - although it sometimes seem to fail to resize the map for some reason.
But the map does not respond to any events at all - it is not even possible to zoom or pan.
As you can see I've tried to give the pages unique IDs by setting the PageId attribute in the ViewBag and render that ID in the layout page. And I can see the markup loading correctly for each page - or so it seems to.
I have not been able to find out, whether every view should be connected to the layout or only the first view should be. What is the right approach to develop a jQuery mobile site with MVC 4?
EDIT The events seem to fire fine when accessing the site on an iPad - but when I access the site through a desktop browser (Chrome) with iPad user agent - no events fire. I would still like your opinion on how to structure pages in MVC4 for jQuery Mobile