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

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

    I had this issue today. I'll add a workaround that uses <script defer> as I didn't see the other answers mention it.

    //on a JS file somewhere (i.e partial-view-caller.js)
    (() => <your partial view script>)();
    
    //in your Partial View
    <script src="~/partial-view-caller.js" defer></script>
    
    //you can actually just straight call your partial view script living in an external file - I just prefer having an initialization method :)
    

    Code above is an excerpt from a quick post I made about this question.

    0 讨论(0)
  • 2020-11-22 06:51

    This is quite a popular question, so I'll post my solution up.

    I had the same problem and although it isn't ideal, I think it actually works quite well and doesn't make the partial dependant on the view.

    My scenario was that an action was accessible by itself but also could be embedded into a a view - a google map.

    In my _layout I have:

    @RenderSection("body_scripts", false)
    

    In my index view I have:

    @Html.Partial("Clients")
    @section body_scripts
    {
        @Html.Partial("Clients_Scripts")
    }
    

    In my clients view I have (all the map and assoc. html):

    @section body_scripts
    {
        @Html.Partial("Clients_Scripts")
    }
    

    My Clients_Scripts view contains the javascript to be rendered onto the page.

    This way my script is isolated and can be rendered into the page where required, with the body_scripts tag only being rendered on the first occurrence that the razor view engine finds it.

    That lets me have everything separated - it's a solution that works quite well for me, others may have issues with it, but it does patch the "by design" hole.

    0 讨论(0)
  • 2020-11-22 06:51

    This worked for me allowing me to co-locate javascript and html for partial view in same file. Helps with thought process to see html and related part in same partial view file.


    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-11-22 06:52

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

    @section Scripts {
    <script>
        $(document).ready(function () {
            ...
        });
    </script>
    }
    
    ...
    
    @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 {
    <script>
        $(document).ready(function () {
            ...
            var partial = @partial;
            $('#partial').html(partial.html);
        });
    </script>
    }
    
    <div id="partial"></div>
    
    0 讨论(0)
  • 2020-11-22 06:52

    choicely, you could use a your Folder/index.cshtml as a masterpage then add section scripts. Then, in your layout you have:

    @RenderSection("scripts", required: false) 
    

    and your index.cshtml:

    @section scripts{
         @Scripts.Render("~/Scripts/file.js")
    }
    

    and it will working over all your partialviews. It work for me

    0 讨论(0)
  • I ran into a nearly identical problem the other day, except that the partial view was a response to an AJAX request. In my situation, the partial was actually a full page, but I wanted it to be accessible as a partial from other pages.

    If you want to render sections in a partial, the cleanest solution is to create a new layout and use a ViewBag variable. This does not work with @Html.Partial() or the new <partial></partial>, use AJAX.

    Main view (that you want to be rendered as a partial elsewhere):

    @if(ViewBag.Partial == true) {
        Layout = "_layoutPartial";
    }
    
    <div>
        [...]
    </div>    
    
    @section Scripts {
        <script type="text/javascript">
            [...]
        </script>
    }
    

    Controller:

    public IActionResult GetPartial() {
    
        ViewBag.Partial = true;
    
        //Do not return PartialView!
        return View("/path/to/view")
    }
    

    _layoutPartial.cshtml (new):

    @RenderSection("Scripts")
    @RenderBody()
    

    Then use AJAX in your page.

    If you want to render the page in the main layout (not a partial), then don't set ViewBag.Partial = true. No HTML helpers necessary.

    0 讨论(0)
提交回复
热议问题