如果我在部分中编写脚本标记,则不会添加java脚本

时间:2016-10-03 07:21:39

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

我正在编写一个MVC4应用程序,我在部分视图中加载脚本,因为它是部分视图特定脚本。如果我在@section scripts中编写脚本标记然后没有添加到页面上,但是如果我删除了@section Scripts那么它在下面工作正常是部分视图中的代码

@section scripts
{    
    <script>
        jQuery.cachedScript('@Url.StaticContent("myscript.js", false)');
    </script>       
}

上面的代码不起作用,就好像我把它写成下面的脚本

<script>
    jQuery.cachedScript('@Url.StaticContent("myscript.js", false)');
</script>

为什么不参考?

1 个答案:

答案 0 :(得分:0)

AFAIK,@section Scripts不适合部分观看。除标准<script>标记方法外,还有两种解决方法:

  1. @section Scripts移动到包含部分视图的视图页面中,并将@RenderSection放置在布局页面上,例如:
  2. _Layout.cshtml

    @RenderSection("Scripts", required: false)
    

    View.cshtml

    @section Scripts
    {
        <script>
            jQuery.cachedScript('@Url.StaticContent("myscript.js", false)');
        </script>
    }
    
    1. 使用此代码为部分视图使用自定义HTML帮助扩展程序(在How to render a Section in a Partial View in MVC3?上使用TagBuilder而不是String.Format来标记呈现给Erik Philips: / p>

      public static class RenderJavaScript
      {
          private const String jsViewData = "RenderJS";
      
          public static void IncludeJS(this HtmlHelper helper, String content)
          {
              List<String> contentList = helper.ViewContext.HttpContext.Items[RenderJavaScript.jsViewData] as List<String>();
              if (contentList != null && contentList.Count > 0)
              {
                  if (!contentList.Contains(url))
                  {
                      contentList.Add(url);
                  }
              }
              else
              {
                  contentList = new List<String>();
                  contentList.Add(url);
                  helper.ViewContext.HttpContext.Items.Add(RenderJavaScript.jsViewData, contentList);
              }
          }
      
          public static MvcHtmlString RenderJS(this HtmlHelper helper)
          {
              var result = new StringBuilder();
      
              List<String> scriptList = helper.ViewContext.HttpContext.Items[RenderJavaScript.jsViewData] as List<String>();
              if (scriptList != null && scriptList.Count > 0)
              {
                  foreach (String script in scriptList)
                  {
                      // start <script> tag
                      TagBuilder startScript = new TagBuilder("script");
                      startScript.MergeAttribute("type", "text/javascript"); // optional
                      startScript.MergeAttribute("src", script); // required src attribute
                      result.AppendLine(startScript.ToString(TagRenderMode.StartTag));
      
                      // end </script> tag
                      TagBuilder endScript = new TagBuilder("script");
                      result.Append(endScript.ToString(TagRenderMode.EndTag));
                  }
              }
      
              return MvcHtmlString.Create(result.ToString());
          }
      
          // rendering inner text instead of using src attribute
          public static MvcHtmlString CustomJS(this HtmlHelper helper)
          {
              var result = new StringBuilder();
      
              List<String> contentList = helper.ViewContext.HttpContext.Items[RenderJavaScript.jsViewData] as List<String>();
              if (contentList != null && contentList.Count > 0)
              {
                  foreach (String text in contentList)
                  {
                      // start <script> tag
                      TagBuilder startScript = new TagBuilder("script");
                      startScript.MergeAttribute("type", "text/javascript"); // optional
                      startScript.SetInnerText(text);
                      result.AppendLine(startScript.ToString(TagRenderMode.StartTag));
      
                      // end </script> tag
                      TagBuilder endScript = new TagBuilder("script");
                      result.Append(endScript.ToString(TagRenderMode.EndTag));
                  }
              }
      
              return MvcHtmlString.Create(result.ToString());
          }
      }
      
    2. 要在视图或部分视图上添加脚本,请使用脚本内容添加IncludeJS方法:

      @{
          Html.IncludeJS(jQuery.cachedScript('@Url.StaticContent("myscript.js", false)'););
      }
      

      或使用脚本路径:

      @{
          Html.IncludeJS(@Url.StaticContent("myscript.js", false));
      }
      

      然后追加RenderJS或&#39; CustomJS&#39;布局页面上的方法:

      @Html.RenderJS()
      @Html.CustomJS()
      

      相关问题:

      How to add a script in a partial view in MVC4?

      ASP MVC Define Section in Partial View