locked
RenderSection RRS feed

  • Question

  • User1979860870 posted

    Hi

      How the below 3 lines works

    @Scripts.Render("~/bundles/jquery", "~/bundles/jqueryval", "~/Scripts/bootstrap.js")

    @RenderSection("featured", required: false)

    @RenderSection("scripts", required: false)

    @Scripts.Render("~/bundles/jquery", "~/bundles/jqueryval", "~/Scripts/bootstrap.js")
    
        <div id="body" class="container">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper container">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - Advancesharp.com :: MVC search, sort, paging, insert, update and delete with Bootstrap Modal Popup </p>
                </div>
            </div>
        </footer>
    
        @RenderSection("scripts", required: false)

    Thanks

    Thursday, May 27, 2021 6:30 AM

All replies

  • User1686398519 posted

    Hi jagjit saini, 

    @Scripts.Render("~/bundles/jquery", "~/bundles/jqueryval", "~/Scripts/bootstrap.js")

    1. The Scripts.Render method can render HTML string containing the script tag or tags for the bundle.
    2. Before using Scripts.Render, you need to create style or script bundles in BundleConfig class under App_Start folder in an ASP.NET MVC project.
      1. BundleConfig
        •     public class BundleConfig
              {
                  public static void RegisterBundles(BundleCollection bundles)
                  {
                      bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                                  "~/Scripts/jquery-{version}.js"));
                      bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                                  "~/Scripts/jquery.validate*"));
                      bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                                  "~/Scripts/modernizr-*")); bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js")); bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap.css", "~/Content/site.css")); }
      2. _Layout
        • <!DOCTYPE html>
          <html>
          <head>
              <meta charset="utf-8" />
              <meta name="viewport" content="width=device-width, initial-scale=1.0">
              <title>@ViewBag.Title - My ASP.NET Application</title>
              @Styles.Render("~/Content/css")
              @Scripts.Render("~/bundles/modernizr")
          </head>
          <body>
                  ... ...
                  @RenderBody()
                  ... ...
              @Scripts.Render("~/bundles/jquery", "~/bundles/bootstrap")
              @RenderSection("scripts", required: false)
          </body>
          </html>
          
    3. Using Bundling and Minification with ASP.NET MVC

    @RenderSection("featured", required: false)

    @RenderSection("scripts", required: false)

    1. @RenderSection is used for injecting content in the defined section.
      1. It allows you to specify a region in Layout.
      2. There are two steps to define @RenderSection in ASP.NET MVC.
        1. Specify a @RenderSection Area in Layout Page.
          • @RenderSection("xxx",false)
        2. Use this specified section to inject content from child page.
          • @section xxx
            {
                this is a RenderSectionArea.
            }
      3. @RenderSection("xxx",false)
        • Regarding the second parameter:
          1. If you use true parameter then it is compulsory to use this section in child page otherwise you will get the error.
          2. If you use false parameter, you can choose to use or not use this section.

    Best Regards,

    YihuiSun

    Friday, May 28, 2021 2:30 AM