Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine

后端 未结 24 1666
无人共我
无人共我 2020-11-22 06:13

I have this section defined in my _Layout.cshtml

@RenderSection(\"Scripts\", false)

I can easily use it from a view:

24条回答
  •  情话喂你
    2020-11-22 06:49

    I solved this a completely different route (because I was in a hurry and didn't want to implement a new HtmlHelper):

    I wrapped my Partial View in a big if-else statement:

    @if ((bool)ViewData["ShouldRenderScripts"] == true){
    // Scripts
    }else{
    // Html
    }
    

    Then, I called the Partial twice with a custom ViewData:

    @Html.Partial("MyPartialView", Model, 
        new ViewDataDictionary { { "ShouldRenderScripts", false } })
    
    @section scripts{
        @Html.Partial("MyPartialView", Model, 
            new ViewDataDictionary { { "ShouldRenderScripts", true } })
    }
    

提交回复
热议问题