How to render JavaScript into MasterLayout section from partial view?

前端 未结 3 1221
小鲜肉
小鲜肉 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"

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

    In Partial View file

    @model SomeType
    
    @{
        var region = ViewData["Region"] as string;
    }
    
    @if (region == "HTMLSection")
    {
    
    
    }
    
    @if (region == "ScriptSection")
    {
            
    
                                     
                  
提交回复
热议问题