I have a simple ASP.Net MVC 4 app designed for mobile use. The application uses the in built C# Visual Studio template for Mobile application which comes with Forms Authentication and an Accounts view and controller for handling the login.
The application has the in built login view, a main menu and a Flight view that I have created myself.
I want all html elements with the CSS class of "date" to be date time pickers so via JQuery UI I have placed the following JavaScript into the Shared/_Layout.cshtml view right after the Jquery bundles are loaded:
<script type="text/javascript">
$(document).ready(function () {
alert("test");
$('.date').datepicker({ dateFormat: "dd/mm/yy" });
});
</script>
This script does exactly what I need when I login into the application and manually change the URL in my browser to the Flight view e.g. manually navigate to http://localhost:63891/Flight
However when I navigate from the link on the main menu it never runs the script. In the main menu view the link is generated with the following:
<ul data-role="listview" data-inset="true">
<li data-role="list-divider">Navigation</li>
<li>@Html.ActionLink("Flights", "Index", "Flight")</li>
</ul>
To be clear clicking the link renders the view as expected it just does not run my JavaScript. The only way I can ever get the script to run is manually entering the URL.
I have tried moving the script inside the actual Flight view but JQuery is not loaded at that point because it is loaded in the scripts section of the Shared/_Llayout.cshtml view.
When I hover the link the URL shown is localhost:63891/Flight but when I actually click through the URL changes to localhost:63891/#/Flight. I'm not sure why this is and if it's relevant.
How do I get the script to run when clicking through links?
EDIT: I think I have this issue but no one has answered his/her question How to handle jQuery event on an ASP.net MVC Ajax loaded page?
Index
action is called when the urlhttp://localhost:63891/Flight
is entered manually? Might it be the case that another action is called, returning another view? – Andrei May 29 '13 at 8:56