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

后端 未结 24 1674
无人共我
无人共我 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:52

    I had a similar problem, where I had a master page as follows:

    @section Scripts {
    
    }
    
    ...
    
    @Html.Partial("_Charts", Model)
    

    but the partial view depended on some JavaScript in the Scripts section. I solved it by encoding the partial view as JSON, loading it into a JavaScript variable and then using this to populate a div, so:

    @{
        var partial = Html.Raw(Json.Encode(new { html = Html.Partial("_Charts", Model).ToString() }));
    }
    
    @section Scripts {
    
    }
    
    

提交回复
热议问题