How to render JavaScript into MasterLayout section from partial view?

前端 未结 3 1120
小鲜肉
小鲜肉 2020-12-01 08:34

Given MVC3 and Razor engine, I got

_MasterLayout.cshtml

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


        
相关标签:
3条回答
  • 2020-12-01 08:58

    This worked for me allowing me to co-locate javascript and html for partial view in same file for ease of readability

    In View which uses Partial View called "_MyPartialView.cshtml"

    <div>
        @Html.Partial("_MyPartialView",< model for partial view>,
                new ViewDataDictionary { { "Region", "HTMLSection" } } })
    </div>
    
    @section scripts{
    
        @Html.Partial("_MyPartialView",<model for partial view>, 
                      new ViewDataDictionary { { "Region", "ScriptSection" } })
    
     }
    

    In Partial View file

    @model SomeType
    
    @{
        var region = ViewData["Region"] as string;
    }
    
    @if (region == "HTMLSection")
    {
    
    
    }
    
    @if (region == "ScriptSection")
    {
            <script type="text/javascript">
        </script">
    }
    
    0 讨论(0)
  • 2020-12-01 09:04

    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

    0 讨论(0)
  • 2020-12-01 09:05

    I don't believe a Partial View can set sections to be used in the layout page :(

    It looks like there are no nice solutions to this - you could include two partials (one for script, one for content):

    <script stuff>
    @Html.RenderAction("PartialViewScripts", "PartialController")
    </script stuff>
    <body stuff>
    @Html.RenderAction("PartialView", "PartialController")
    </body stuff>
    
    0 讨论(0)
提交回复
热议问题