我正在编写一个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>
为什么不参考?
答案 0 :(得分:0)
AFAIK,@section Scripts
不适合部分观看。除标准<script>
标记方法外,还有两种解决方法:
@section Scripts
移动到包含部分视图的视图页面中,并将@RenderSection
放置在布局页面上,例如:_Layout.cshtml
@RenderSection("Scripts", required: false)
View.cshtml
@section Scripts
{
<script>
jQuery.cachedScript('@Url.StaticContent("myscript.js", false)');
</script>
}
使用此代码为部分视图使用自定义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());
}
}
要在视图或部分视图上添加脚本,请使用脚本内容添加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()
相关问题: