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

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

    If you do have a legitimate need to run some js from a partial, here's how you could do it, jQuery is required:

    <script type="text/javascript">        
        function scriptToExecute()
        {
            //The script you want to execute when page is ready.           
        }
    
        function runWhenReady()
        {
            if (window.$)
                scriptToExecute();                                   
            else
                setTimeout(runWhenReady, 100);
        }
        runWhenReady();
    </script>
    
    0 讨论(0)
  • 2020-11-22 06:42

    I had the similar problem solved it with this:

    @section ***{
    @RenderSection("****", required: false)
    }
    

    Thats a pretty way to inject i guesse.

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

    I had this problem and used this technique.

    Its the best solution i found which is very flexible.

    Also please vote here to add support for cumulative section declaration

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

    Sections don't work in partial views and that's by design. You may use some custom helpers to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend using the @scripts section of the main view to do that and not have the partials worry about scripts.

    0 讨论(0)
  • 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 } })
    }
    
    0 讨论(0)
  • 2020-11-22 06:50

    The goal of the OP is that he wants to define inline scripts into his Partial View, which I assume that this script is specific only to that Partial View, and have that block included into his script section.

    I get that he wants to have that Partial View to be self contained. The idea is similar to components when using Angular.

    My way would be to just keep the scripts inside the Partial View as is. Now the problem with that is when calling Partial View, it may execute the script in there before all other scripts (which is typically added to the bottom of the layout page). In that case, you just have the Partial View script wait for the other scripts. There are several ways to do this. The simplest one, which I've used before, is using an event on body.

    On my layout, I would have something on the bottom like this:

    // global scripts
    <script src="js/jquery.min.js"></script>
    // view scripts
    @RenderSection("scripts", false)
    // then finally trigger partial view scripts
    <script>
      (function(){
        document.querySelector('body').dispatchEvent(new Event('scriptsLoaded'));
      })();
    </script>
    

    Then on my Partial View (at the bottom):

    <script>
      (function(){
        document.querySelector('body').addEventListener('scriptsLoaded', function() {
    
          // .. do your thing here
    
        });
      })();
    </script>
    

    Another solution is using a stack to push all your scripts, and call each one at the end. Other solution, as mentioned already, is RequireJS/AMD pattern, which works really well also.

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