您可以结合使用两个助手:
public static class HtmlExtensions{ public static MvcHtmlString script(this HtmlHelper htmlHelper, Func<object, HelperResult> template) { htmlHelper.ViewContext.HttpContext.Items["_script_" + Guid.NewGuid()] = template; return MvcHtmlString.Empty; } public static IHtmlString Renderscripts(this HtmlHelper htmlHelper) { foreach (object key in htmlHelper.ViewContext.HttpContext.Items.Keys) { if (key.ToString().StartsWith("_script_")) { var template = htmlHelper.ViewContext.HttpContext.Items[key] as Func<object, HelperResult>; if (template != null) { htmlHelper.ViewContext.Writer.Write(template(null)); } } } return MvcHtmlString.Empty; }}然后在您的
_Layout.cshtml:
<body>...@Html.Renderscripts()</body>
在某些模板中:
@Html.script( @<script src="@Url.Content("~/scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>)


