【问题标题】:How to access section from extension method?如何从扩展方法访问部分?
【发布时间】:2013-06-05 05:47:52
【问题描述】:

我正在为 javascript 呈现的图表编写 html 扩展 (Razor)。 我可以编辑 javascript 以从数据属性中读取大多数值,但有时我需要将内联 javascript 直接插入页面并链接库。我想让它自动化。 有没有办法通过 html helper 从扩展方法访问一个部分(如 @RenderSection("Scripts", false) )?

谢谢

【问题讨论】:

    标签: javascript asp.net-mvc extension-methods


    【解决方案1】:

    RenderSection 是 WebPageBase 的常规方法,因此您可以在帮助程序中使用它。这里有一个 sn-p:

    public static class HtmlExtensions
    {
        public static HelperResult InvokeRenderSection(this HtmlHelper html)
        {
            var view = (WebPageBase)html.ViewDataContainer;
            var result = view.RenderSection("scripts", false);
            return result;
        }
    }
    

    【讨论】:

    • 很酷,但是我怎样才能写到部分呢?当我尝试调用result.WriteTo() 方法时,我得到The file "~/Views/Home/Index.cshtml" cannot be requested directly because it calls the "RenderSection" method。似乎没有办法从 html 辅助方法写入部分。
    猜你喜欢
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多