1

I am new to ASP.NET Core and Bootstrap. I am trying to display a carousel on the index page only and have it fit the whole screen.

I tried adding the carousel to the _Layout.cshtml file and it fit the screen how I wanted it to, but it was visible on all the pages.

Then I tried adding the carousel to the index.cshtml file, but the carousel didn't fit the whole page.

Is there a way to get the carousel to be visible only on the index page and fit the whole screen?

Carousel Code

<div id="myCarousel" class="carousel slide" data-ride="carousel" style="margin-top:-15px">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner" role="listbox">
        <div class="carousel-item active">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img1" />
            <div class="carousel-caption">
                <h3>Image 1</h3>
                <p>A happy image</p>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img2" />
            <div class="carousel-caption">
                <h3>Image 2</h3>
                <p>A new image</p>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img3" />
            <div class="carousel-caption">
                <h3>Image 3</h3>
                <p>An old image</p>
            </div>
        </div>
    </div>
    <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

Carousel when coded in Index.cshtml

Carousel when coded in _Layout.cshtml

I'd like to combine the good points of those 2 versions and get the best of both worlds.

Thanks in advance!

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26

1 Answers1

0

Add this to your Layout page.

@RenderSection("style", required: false)

Now add your Carousel code to any View page. under @section style{ "your bootstrap carousel code"} so for that, you will successfully show your carousel where you want. also, the carousel fits the whole page.

Index.cshtml

@section style{

<div id="myCarousel" class="carousel slide" data-ride="carousel" style="margin-top:-15px">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
        <li data-target="#myCarousel" data-slide-to="1"></li>
        <li data-target="#myCarousel" data-slide-to="2"></li>
    </ol>

    <div class="carousel-inner" role="listbox">
        <div class="carousel-item active">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img1" />
            <div class="carousel-caption">
                <h3>Image 1</h3>
                <p>A happy image</p>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img2" />
            <div class="carousel-caption">
                <h3>Image 2</h3>
                <p>A new image</p>
            </div>
        </div>
        <div class="carousel-item">
            <img class="d-block w-100" style="background-color: gray; height: 550px; width: 100%;" src="~/images/family1.jpeg" alt="Img3" />
            <div class="carousel-caption">
                <h3>Image 3</h3>
                <p>An old image</p>
            </div>
        </div>
    </div>
    <a class="carousel-control-prev" href="#myCarousel" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#myCarousel" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

}
Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26