在索引视图中找到Javascript函数,但不在其他视图中找到

时间:2012-11-08 12:32:26

标签: javascript controller asp.net-mvc-4

我正在使用ASP.NET MVC 4来开发Web应用程序。我有一个外部javascript文件,其中包含一个方法initializeLocation()。如果我在我的Home控制器的Index视图中引用该文件,它可以正常工作。但是当我尝试在另一个视图中引用JS文件,并在body onLoad中调用该方法时,我得到以下错误:

Uncaught ReferenceError: initializeLocation is not defined

以下是我的观点代码:

@{
    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>

这是我的控制器代码:

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();
        }

    }
}

我是MVC的新手,我仍然习惯于控制器等概念,但是不明白为什么完全相同的代码应该在一个视图中工作而不是另一个视图。

1 个答案:

答案 0 :(得分:1)

而不是

<script src="~/Scripts/jquery-1.8.2.js" type="text/javascript"></script>

<script type="text/javascript" src="~/Scripts/locationFinder.js"></script>

使用

<script src="@Url.Content("~/Scripts/jquery-1.8.2.js")" type="text/javascript"></script>

<script type="text/javascript" src="@Url.Content("~/Scripts/locationFinder.js")" ></script>

问题是在页面上呈现的网址。使用@Url.Content使用其rool路径引用呈现URL。在路由时它会呈现正确的URL。