在那里把脚本MVC _layout页 [英] Where to put scripts in MVC _layout page

查看:145
本文介绍了在那里把脚本MVC _layout页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我从本网站的脚本应该在_layout页面底部的阅读理解的建议。我的问题是我不知道确切位置的'底部'的。有些人说这只是一个标记之前但是,这并不为我工作。我试图把剧本在很多地方却无处似乎工作。可能有人请告诉我,我做错了。

这是我的关于页

  @ {
    ViewBag.Title =关于我们;
}< H2>关于< / H>
&所述p为H.;
     把内容在这里。
&所述; / P><脚本类型=文/ JavaScript的>
  $(函数(){
    警报(的Hello World);
  });
< / SCRIPT>

这是我的_layout页

 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
  <标题> @ ViewBag.Title< /标题>
  <链接HREF =@ Url.Content(〜/内容/的site.css)的rel =stylesheet属性类型=文/ CSS/>
  @ * jQuery的工作,如果这条线是在这里但如果它是在底部
   <脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.6.2.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
  * @
< /头>
<身体GT;
  < D​​IV CLASS =页>
    < D​​IV ID =头>
      < D​​IV ID =标题>
        < H1>
          我的MVC应用程序< / H1>
      < / DIV>
      < D​​IV ID =logindisplay>
        @ Html.Partial(_ LogOnPartial)
      < / DIV>
      < D​​IV ID =MenuContainer中>
        < UL ID =菜单>
          <立GT; @ Html.ActionLink(家,索引,家庭)< /李>
          <立GT; @ Html.ActionLink(关于,关​​于,家庭)< /李>
        < / UL>
      < / DIV>
    < / DIV>
    < D​​IV ID =主>
      @RenderBody()
    < / DIV>
    < D​​IV ID =页脚>
    < / DIV>
  < / DIV>
  @ *如果脚本是在这里jQery不工作! * @
  <脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.6.2.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
< /身体GT;
< / HTML>


解决方案

jQuery的文件有之前的文件中找到第一个引用的jQuery包括在内。因此,要解决这个问题,我把头部jQuery的文件,未在视图,但在其他脚本文件使用关闭之前都包含所有其他脚本主体标签

Hello I understand from reading advice on this site that scripts should go at the bottom of the _layout page. My problem is that I am not sure exactly where the 'bottom' is. Some people have said it is just before the tag but that does not work for me. I have tried putting the script in many places but nowhere seems to work. Could someone please tell me what I am doing wrong.

This is my About page

@{
    ViewBag.Title = "About Us";
}

<h2>About</h2>
<p>
     Put content here.
</p>

<script type="text/javascript">
  $(function () {
    alert("Hello World");
  });
</script>

This is my _Layout page

<!DOCTYPE html>
<html>
<head>
  <title>@ViewBag.Title</title>
  <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
  @* jQuery works if this line is here but not if it is at the bottom
   <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
  *@
</head>
<body>
  <div class="page">
    <div id="header">
      <div id="title">
        <h1>
          My MVC Application</h1>
      </div>
      <div id="logindisplay">
        @Html.Partial("_LogOnPartial")
      </div>
      <div id="menucontainer">
        <ul id="menu">
          <li>@Html.ActionLink("Home", "Index", "Home")</li>
          <li>@Html.ActionLink("About", "About", "Home")</li>
        </ul>
      </div>
    </div>
    <div id="main">
      @RenderBody()
    </div>
    <div id="footer">
    </div>
  </div>
  @* jQery does not work if the script is here! *@
  <script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript"></script>
</body>
</html>

解决方案

Jquery file has to be included before first reference to jquery is found on the document. So, to solve this problem, i put jquery file in head section and all other script that are not used in views but in other script files are included before closing body tag

这篇关于在那里把脚本MVC _layout页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆