English 中文(简体)
如何将部分视图中的 HtmlHelper 扩展生成的 JavaScript 和添加到父视图中?
原标题:How to concatenate JavaScript generated with HtmlHelper extension inside PartialViews and add to parent View?

我有2部分意见 在家长观点中。

在这2个PartiaPiview I 中,我生成了一些 JavaScript 代码, 需要包含在父视图底部 。

这是 HtmlHelper 扩展名, 生成部分意见中的 JavaScript 。

public static MvcHtmlString KO_ObjectFor<TModel>(this HtmlHelper<TModel> htmlHelper)
{
    String str = "some javascript code";
    return MvcHtmlString.Create(str);
}

这是父视图( 主视图)

...
//First Partial View
<% Html.RenderPartial("_PartialViewFirst"); %>

//Second Partial View
<% Html.RenderPartial("_PartialViewSecond"); %>

//Here I need to display the JavaScript from the PartialViews
<script type="text/javascript">
   <%: Html.FunctionToConcatenateJavaScript() %>
</script>
...

`强'什么是正确的方法?

最佳回答

您可以在 HttpContext 中使用堆叠来推动和播放脚本 :

public static class HtmlExtensions
{
    private const string ScriptsKey = "__scripts__";

    public static MvcHtmlString KO_ObjectFor<TModel>(this HtmlHelper<TModel> htmlHelper)
    {
        var stack = htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] as Stack<string>;
        if (stack == null)
        {
            stack = new Stack<string>();
            htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] = stack;
        }
        String str = "some javascript code";
        stack.Push(str);

        return new HtmlString("some code that the helper needs to generate and output to the view");
    }

    public static IHtmlString FunctionToConcatenateJavaScript(this HtmlHelper htmlHelper)
    {
        var stack = htmlHelper.ViewContext.HttpContext.Items[ScriptsKey] as Stack<string>;
        if (stack == null)
        {
            return MvcHtmlString.Empty;
        }

        var scriptTag = new TagBuilder("script");
        scriptTag.Attributes["type"] = "text/javascript";
        var sb = new StringBuilder();
        foreach (var script in stack)
        {
            sb.AppendLine(script);
        }
        scriptTag.InnerHtml = sb.ToString();

        return new HtmlString(scriptTag.ToString());
    }
}

然后,你可以推:

<%= Html.KO_ObjectFor(x => x.Foo) %>
<%= Html.KO_ObjectFor(x => x.Bar) %>
...

然后在主人身上弹出全部的EM:

<%= Html.FunctionToConcatenateJavaScript() %>
问题回答

也许你可以这样:

部分1:

@{
    ViewBag.JS = @Html.KO_ObjectFor(firstJs);
}

部分2:

@{
    ViewBag.JS += @Html.KO_ObjectFor(secondJs);
}

主要:

<script type="text/javascript">
   @ViewBag.JS
</script>

抱歉, 剃刀语法





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签