2011-10-06 60 views
2

从部分视图添加外部文件的最佳方式是什么?从部分视图向布局页面添加js和css文件

我需要做的是这样的局部视图这个

代码:

@{ 
    var files = new List<string>(); 
    files.Add("some path"); 
    files.Add("some path"); 
    ViewBag.Files = files; 
} 

在页面布局

@foreach (var file in ViewBag.Files) { 
    @file 
} 

,这实际上并不工作,但

+0

外部文件的内容是什么? 'css'? 'js'?你不会更好用一个简单的'client.css'和一个'client.js'在哪里你可以动态地添加任何东西? - 让我知道什么样的文件,我会告诉你我在做什么。 – balexandre

+0

@balexandre我创建了一个简单的cms,开发人员应该能够创建插件为部分视图(编辑器模板)。插件可以包含特定插件的css和js。你和我在一起吗? – Marcus

+0

@balexandre你能告诉我你的解决方案是如何工作的吗? – Marcus

回答

6

作为承诺

您不能渲染@section,因为它在Partial Views渲染时不受支持,直到您可以执行此操作:

_Layout.cshtml

@RenderSection("scripts", false) 
@Html.RenderSection("scripts") 

第一个是默认,第二行是你的品牌渲染部分新的方式,我用这两个在我的代码...

现在,让我们添加一些代码到我们管窥

,而不是

@section scripts { 
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
} 

将其替换为:

@Html.Section(
    @<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>, "scripts" 
) 
@Html.Section(
    @<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>, "scripts" 
) 

和我们小帮手,你只是把里面的您的Models文件夹,并在部分视图和布局中引用它第

// using idea from http://stackoverflow.com/questions/5433531/using-sections-in-editor-display-templates/5433722#5433722 
public static class HtmlExtensions 
{ 
    public static MvcHtmlString Section(this HtmlHelper htmlHelper, Func<object, HelperResult> template, string addToSection) 
    { 
     htmlHelper.ViewContext.HttpContext.Items[String.Concat("_", addToSection, "_", Guid.NewGuid())] = template; 
     return MvcHtmlString.Empty; 
    } 

    public static IHtmlString RenderSection(this HtmlHelper htmlHelper, string sectionName) 
    { 
     foreach (object key in htmlHelper.ViewContext.HttpContext.Items.Keys) 
     { 
     if (key.ToString().StartsWith(String.Concat("_", sectionName, "_"))) 
     { 
      var template = htmlHelper.ViewContext.HttpContext.Items[key] as Func<object, HelperResult>; 
      if (template != null) 
      { 
       htmlHelper.ViewContext.Writer.Write(template(null)); 
      } 
     } 
     } 
     return MvcHtmlString.Empty; 
    } 
} 

要渲染CSS,所有你需要这样使用其他部分的名称,例如:

_Layout.cshtml

@Html.RenderSection("styles") 
在局部视图

@Html.Section(
    @<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">, "styles" 
) 

我希望这有助于。

+0

这个解决方案似乎工作很好,我想我需要把@ Html.RenderSection(“样式”)之前得到这个正常工作,因为局部视图需要在RenderSection 。 – Marcus

+0

您好,我有一个关于这个解决方案的延迟问题。这看起来像我以随机顺序呈现的部分?例如添加多个脚本会对我造成一些问题。 – Marcus

1

而不是持有Viewbag中的值尝试保存HttpContext.Current.Items中的值是请求范围。

像这样HttpContext.Current.Items.Add("files", files)

+0

我永远不会使用它,因为在处理多个场景时,您可以轻松地释放上下文,对于沉重的东西,您应该使用'TempData',但是要保存一个字符串列表......'ViewBag'或'ViewData'已经绰绰有余! – balexandre

+0

如果使用ViewBag或ViewData,则无法在Layout中访问保存的值。 TempData会一直工作到下一个请求,并且会导致错误的结果。我想知道什么样的情景会导致背景丢失?我实际上是在我自己的应用程序中使用它,并且运行良好。 – AnyOne

0

也许你可以使用部分为。在您的布局中:

<head> 
... 
    @RenderSection("Head", false) 
</head> 

false参数表示该部分不是必需的。

在你的部分观点,你做这样的事情:

@section Head 
{ 
    foreach(file in Model.Files} 
    { 
     .... render css/js links here 
    } 
} 

这适用于看法,并不完全肯定它的工作原理与partialviews,但它应该:)

+1

这不工作从部分意见 – Marcus

相关问题