包含仅对视图具有全局依赖性的本地javascript文件

时间:2014-07-21 05:33:29

标签: asp.net-mvc-4 razor

我有这样的观点

@model IEnumerable<DuckingOctoBear.Models.PostViewModel>

<p>
  @if (User.IsInRole("Administrator") || User.IsInRole("Editor"))
  {
    @Html.ActionLink("Create New", "Create")
  }
</p>

<div class="">
  @foreach (DuckingOctoBear.Models.PostViewModel item in Model)
  {
    <a href="/Posts/Details/@item.Post.Id" class="post-element">
      <h4>@Html.DisplayFor(modelItem => item.Post.Title)</h4>
      <h6>
        Inserted by @item.User.UserName at @item.Post.Date.ToString("dd MMMM yyyy hh:ss")
      </h6>
      <article>
        @Html.Raw(item.Post.Text)
      </article>
      <span>
        @if (User.IsInRole("Administrator") || User.IsInRole("Editor"))
        {
          @Html.ActionLink("Edit", "Edit", new { id = item.Post.Id })
          <span>|</span>
        }

        @Html.ActionLink("Details", "Details", new { id = item.Post.Id })
        <span>|</span>

        @if (User.IsInRole("Administrator"))
        {
          @Html.ActionLink("Delete", "Delete", new { id = item.Post.Id })
          <span>|</span>
        }
      </span>
    </a>
  }
</div>

...我想要包含一个Javascript文件,该文件与 _Layout.cshtml

中已包含的其他Javascript文件有很强的依赖关系
<div class="container body-content">

    @RenderBody()
    <hr />
    <footer>
      <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
  </div>

  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")

如何在不将其注册到捆绑包中的情况下包含该本地文件,对所有视图都是如此?

1 个答案:

答案 0 :(得分:0)

好的史诗失败

这个_Layout.cshtml

<div class="container body-content">
    @RenderBody()
    <hr />
    <footer>
      <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
    </footer>
  </div>

  @Scripts.Render("~/bundles/jquery")
  @Scripts.Render("~/bundles/bootstrap")
  @RenderSection("scripts", required: false)

我只能添加一个新的部分

@RenderSection("LocallyScriptLibrary", required: false)

然后在目标视图中执行:

@section LocalScriptLibrary{
  <script src="~/Scripts/LocalLibrary.js"></script>
}