I am using ASP.NET MVC 4 to develop a web application. I have an external javascript file which contains a method initializeLocation(). If I reference the file in the Index view of my Home controller it works fine. But when I try to reference the JS file in another view, and call the method in the body onLoad, then I get the following error:
Uncaught ReferenceError: initializeLocation is not defined
Here is the code of my view:
@{
ViewBag.Title = "Online Users";
}
<html>
<head>
<title>Online Users</title>
</head>
<body onload="initializeLocation()">
<h2>Online Users</h2>
<div id="wrapper">
<div id="upperPanel">
<div>
<ul id="onlineUsers" itemid="@HttpContext.Current.User.Identity.Name">
</ul>
</div>
<div id="friends">
</div>
</div>
<div id="bottomPanel">
<input id="submitLocation" type="submit" value="Share Location" style="margin-left: 10px;" /><br />
</div>
</div>
<label id="locLabel"></label>
<div id="map" style="width: 100%; height: 600px"></div>
<script>
var Pusher_APP_KEY = 'b5ee1a1486b7f0cec06f';
</script>
<script src="http://js.pusher.com/1.12/pusher.min.js"></script>
<script src="~/Scripts/jquery-1.8.2.js" type="text/javascript"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBsjVTOfgW39medqXn6cmOTfVyyxIX3Nl8&sensor=true"> </script>
<script type="text/javascript">
//postURL is used in locationFinder.js to set the URL of POST requests
//It is declared here to be able to use a separate java file instead of embedding it
var postURL = '@Url.Action("Index", "Home")';
</script>
<script type="text/javascript" src="~/Scripts/locationFinder.js">
</script>
</body>
</html>
And here is my controller code:
namespace LBSPrototype1.Controllers
{
public class HomeController : Controller
{
private static readonly PusherProvider Provider = new PusherProvider
(
ConfigurationManager.AppSettings["pusher_app_id"],
ConfigurationManager.AppSettings["pusher_key"],
ConfigurationManager.AppSettings["pusher_secret"]
);
public ActionResult Index(string latitude, string longitude, string username)
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
//
// GET: /Home/OnlineUsers
[AllowAnonymous]
public ActionResult OnlineUsers()
{
return View();
}
//
// POST: /Home/OnlineUsers
[AllowAnonymous]
[HttpPost]
public ActionResult OnlineUsers(string latitude, string longitude, string username)
{
var now = DateTime.UtcNow;
var request = new ObjectPusherRequest(
"chat_channel",
"message_received",
new
{
lat = latitude,
lon = longitude,
user = username,
timestamp = now.ToShortDateString() + " " + now.ToShortTimeString()
});
Provider.Trigger(request);
return View();
}
}
}
I’m new to MVC and am still getting used to the concept of controllers and such, but can’t see why the exact same code should work in one view and not the other.
Instead of
Use
The problem is the url which is render on the page. use
@Url.Contentto render a url with its rool path reference. it will render correct url while in routing.