从部分填充剃刀部分 [英] Populate a Razor Section From a Partial

查看:21
本文介绍了从部分填充剃刀部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这样做的主要动机是获取 Javascript,该 Javascript 仅在页面底部的部分与其余 Javascript 一起需要,而不是在呈现部分的页面中间.

My main motivation for trying to do this is to get Javascript that is only required by a partial at the bottom of the page with the rest of the Javascript and not in the middle of the page where the partial is rendered.

这是我正在尝试做的一个简化示例:

Here's a simplified example of what I'm trying to do:

这是在正文之前带有脚本部分的布局.

Here is the layout with a Scripts section right before the body.

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />    
</head>

<body>
    @RenderBody()
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    @RenderSection("Scripts", false)
</body>
</html>

这是使用此布局的示例视图.

Here's an example view using this layout.

<h2>This is the view</h2>

@{Html.RenderPartial("_Partial");}

@section Scripts {
<script type="text/javascript">
        alert("I'm a view.");
</script>
}

这是从视图中呈现的部分.

And here's the partial being rendered from the view.

<p>This is the partial.</p>

@* this never makes it into the rendered page *@
@section Scripts {
<script type="text/javascript">
    alert("I'm a partial."); 
</script>
}

在此示例中,视图中指定的标记被放置到部分中,但部分中的标记未放置.是否可以使用 Razor 从局部视图填充部分?如果没有,还有哪些其他方法可以获取仅页面底部的部分需要的 Javascript,而无需全局包含它?

In this example, the markup specified in the view is placed into the section, but the markup from the partial is not. Is it possible to populate a section from a partial view with Razor? If not, what are some other methods of getting Javascript that's only needed by partials at the bottom of the page without including it globally?

推荐答案

我处理这个问题的方法是为 HtmlHelper 类编写几个扩展方法.这允许局部视图说它们需要一个脚本,然后在我调用辅助方法的布局视图中写入标记以发出所需的脚本

The way I dealt with this is to write a couple extension methods to the HtmlHelper class. That allows partials views to say that they require a script, and then in the layout view that writes the tag I call to my helper method to emit the required scripts

以下是辅助方法:

public static string RequireScript(this HtmlHelper html, string path, int priority = 1)
{
    var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List<ResourceInclude>;
    if (requiredScripts == null) HttpContext.Current.Items["RequiredScripts"] = requiredScripts = new List<ResourceInclude>();
    if (!requiredScripts.Any(i => i.Path == path)) requiredScripts.Add(new ResourceInclude() { Path = path, Priority = priority });
    return null;
}

public static HtmlString EmitRequiredScripts(this HtmlHelper html)
{
    var requiredScripts = HttpContext.Current.Items["RequiredScripts"] as List<ResourceInclude>;
    if (requiredScripts == null) return null;
    StringBuilder sb = new StringBuilder();
    foreach (var item in requiredScripts.OrderByDescending(i => i.Priority))
    {
        sb.AppendFormat("<script src="{0}" type="text/javascript"></script>
", item.Path);
    }
    return new HtmlString(sb.ToString());
}
public class ResourceInclude
{
    public string Path { get; set; }
    public int Priority { get; set; }
}

一旦你有了它,你的局部视图只需要调用 @Html.RequireScript("/Path/To/Script").

Once you have that in place your partial view just needs to call @Html.RequireScript("/Path/To/Script").

在布局视图的 head 部分,您调用 @Html.EmitRequiredScripts().

And in the layout view's head section you call @Html.EmitRequiredScripts().

另外一个好处是它允许您清除重复的脚本请求.如果您有多个视图/部分视图需要给定的脚本,您可以放心地假设您只会输出一次

An added bonus of this is that it allows you to weed out duplicate script requests. If you have multiple views/partial views that need a given script you can safely assume that you will only output it once

这篇关于从部分填充剃刀部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆