Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - How to render JavaScript into MasterLayout section from partial view?

Given MVC3 and Razor engine, I got

_MasterLayout.cshtml

@RenderSection("JavaScript", required: false)
..
..
@RenderBody()
..

View.cshtml with _MasterLayout.cshtml defined in _ViewStart.cshtml

..
@Html.RenderAction("PartialView", "PartialController")
..

PartialView.cshtml

..
@section JavaScript
{
........
}
..

How can I make sure that JavaScript from Partial View ends up in the Master Layout section?

Edit

The above scenario doesn't work because the partial view doesn't have a master layout defined. View, on the other hand, does have Layout with RenderSection defined. If I move section JavaScript from Partial View to View, Razor knows where to render it to. But, since partial view doesn't have a layout, it doesn't know what to do with section JavaScript and thus doesn't render it anywhere.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I created a set of extension methods to render scripts blocks (or anything) from a partial, into the main layout.

public static class ViewPageExtensions
{  
 private const string SCRIPTBLOCK_BUILDER = "ScriptBlockBuilder";

    public static MvcHtmlString ScriptBlock(
        this WebViewPage webPage,
        Func<dynamic, HelperResult> template)
    {
        if (!webPage.IsAjax)
        {
            var scriptBuilder = webPage.Context.Items[SCRIPTBLOCK_BUILDER] 
                                as StringBuilder ?? new StringBuilder();

            scriptBuilder.Append(template(null).ToHtmlString());

            webPage.Context.Items[SCRIPTBLOCK_BUILDER] = scriptBuilder;

            return new MvcHtmlString(string.Empty);
        }
        return new MvcHtmlString(template(null).ToHtmlString());
    }

    public static MvcHtmlString WriteScriptBlocks(this WebViewPage webPage)
    {
        var scriptBuilder = webPage.Context.Items[SCRIPTBLOCK_BUILDER] 
                            as StringBuilder ?? new StringBuilder();

        return new MvcHtmlString(scriptBuilder.ToString());
    }
}

Then it can be used like so:

@this.ScriptBlock(
@<script  type='text/javascript'>
    $(document).ready(function () {
        notify('@message', '@Model.Type.ToString()',                    
               '@Model.Type.ToString().ToLower()');
    });
</script>)

And then rendered in the main layout:

@this.WriteScriptBlocks()

Check out the full article here: A Script-Block Templated Delegate for Inline-Scripts in Razor Partials


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...