locked
Problem with bootstrap's carousel in Razor RRS feed

  • Question

  • User-1753836934 posted

    Hi 

    I need help for a boostrap/razor project in school. I'm totaly new with those technologies. I've listen to youtube videos for help. I learn how to add bootstrap's libraires with libman in visual studio and i also copied the code from a video that show how the bootstrap' carousel is done. It doesnt work as expected the 3 pictures in my images folder are show one under of each other and there are show all the 3 at the same time instead of one picture slide each 2 seconds. 

    Thanks for your help

    ********************************************************************_Layout.cshtml

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - ProjetFinal</title>
        <link href="~/lib/bootstrap/css/bootstrap.css" rel="stylesheet" />
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" />
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">ProjetFinal</a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
          <div class="constainer">     
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
          </div>

        <footer class="border-top footer text-muted">
            <div class="container">
                &copy; 2021 - ProjetFinal - <a asp-area="" asp-page="/Privacy">Privacy</a>
            </div>
        </footer>

        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>

        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>

    ********************************************************************index.cshtml

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>


    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div id="imageCarousel" class="carousel slide" data-interval="2000" data="ride-carousel">
                    <div class="carousel-inner">
                      <div class="item-active">  
                            <img src="~/images/page1.jpg" class="img-responsive" />
                      </div>
                       <div class="item">
                            <img src="~/images/entraide-696x367.jpg" class="img-responsive" />
                       </div>
                       <div class="item">
                            <img src="~/images/page0AI.jpg" class="img-responsive" />
                       </div> 
                    </div>
                </div>

             </div>
        </div>
     </div>
     </div>   

    Wednesday, April 14, 2021 6:41 PM

Answers

  • User-1753836934 posted

    Hi Xudong

    You nailed it:  i have an error  in the chrome console : bootstrap.mn.js:1 Failed to load resource: the server responded with a status of 404 ().

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>  there was an error in my code for that line.

    IT IS WORKING  YEAH!  Many thanks for your help Xudong

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 16, 2021 5:16 AM

All replies

  • User-939850651 posted

    Hi alphamu,

    According to your description, I created a simple demo to reproduce your issue, I found this related to the introduced resources.

    You could try add js and css resources like this:

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div id="imageCarousel" class="carousel slide" data-interval="2000" data-ride="carousel">
                        <div class="carousel-inner">
                            <div class="item active">
                                <img src="~/images/2.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/3.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/4.png" class="img-responsive" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    You can also download related resources as static resources and place them in the project.

    Best regards,

    Xudong Peng

    Thursday, April 15, 2021 3:38 AM
  • User-1753836934 posted

    Hi alphamu,

    According to your description, I created a simple demo to reproduce your issue, I found this related to the introduced resources.

    You could try add js and css resources like this:

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div id="imageCarousel" class="carousel slide" data-interval="2000" data-ride="carousel">
                        <div class="carousel-inner">
                            <div class="item active">
                                <img src="~/images/2.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/3.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/4.png" class="img-responsive" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    You can also download related resources as static resources and place them in the project.

    Best regards,

    Xudong Peng

    *****************************************************************************************************************************************

    Hi Xudong

    First thanks for your answer. But it still doesnt work. it's a little better i see one picture but it doesnt change. I'm also gonna send the _layout.cshtml file. I change the pictures because they didn't have the same dimensions.

    index.cshtml

    *********************************************************************************************************************************************************

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.mn.js"></script>

    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>


        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <div id="imageCarousel" class="carousel slide" data-interval="2000" data="ride-carousel">
                        <div class="carousel-inner">
                            <div class="item-active">
                                <img src="~/images/A1.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/A2.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/A3.png" class="img-responsive" />
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

    *****************************************************************************************************************************************************

    _Layout.cshtml

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - ProjetFinal</title>

    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">ProjetFinal</a>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
           <div class="container">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
           </div> 

        <footer class="border-top footer text-muted">
            <div class="container">
                &copy; 2021 - ProjetFinal - <a asp-area="" asp-page="/Privacy">Privacy</a>
            </div>
        </footer>

        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>

    Thursday, April 15, 2021 4:57 AM
  • User-939850651 posted

    Hi alphamu,

    Well, what I mentioned above is the main problem, I forgot to remind you that there are some minor errors in the page code.

    For example, the style class should be active instead of item-active.

    Something like this:

    <div id="imageCarousel" class="carousel slide" data-interval="2000" data-ride="carousel">
                        <div class="carousel-inner">
                            <div class="item active">
                                <img src="~/images/2.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/3.png" class="img-responsive" />
                            </div>
                            <div class="item">
                                <img src="~/images/4.png" class="img-responsive" />
                            </div>
                        </div>
                    </div>

    For the use of these attributes and classes, you could refer to w3schools bootstrap_carousel.asp.

    Best regards,

    Xudong Peng

    Thursday, April 15, 2021 8:02 AM
  • User-1753836934 posted

    Hi Xudong, thanks again to support a newbie like me. I copied the code from w3schools and make only change for the pictures but it's still doesnt move. The first picture only show. Also the controls dont show.

    Thanks for your help.

    Index.cshtml

    **************************************************************************************************************************************************

    @model IndexModel
    @{
    @page
        ViewData["Title"] = "Home page";
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.mn.js"></script>

       <div class="text-center">
          <h1 class="display-4">Welcome</h1>
           <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>


        <div id="myCarousel" class="carousel slide" data-ride="carousel">
            <!-- Indicators -->
            <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>

            <!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active">
                    <img src="~/images/A1.png">
                </div>

                <div class="item">
                    <img src="~/images/A2.png">
                </div>

                <div class="item">
                    <img src="~/images/A3.png">
                </div>
            </div>

            <!-- Left and right controls -->
            <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#myCarousel" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>
    </div>

    ***********************************************************************************************************************************************

    Thursday, April 15, 2021 12:18 PM
  • User-939850651 posted

    Hi alphamu,

    The problem may be that online resources are not obtained correctly. Do you see such an error message in the browser console column (press F12 in the browser)?

    It is indeed a bit strange, in the previous test, such a problem did not appear. But the problem is still the same as I mentioned, you need to replace these resources to make the carousel work. Regarding these commonly used js and css resources, I recommand that you download them and put them in the project path (wwwroot) as static files. Because you can't ensure that online resources always work (like the current problem).

    When I changed the resource, it worked again.

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    @*<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.mn.js"></script>*@
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

    If it’s other issues, can you provide more details?

    Best regards,

    Xudong Peng

    Friday, April 16, 2021 1:54 AM
  • User-1753836934 posted

    Hi Xudong

    You nailed it:  i have an error  in the chrome console : bootstrap.mn.js:1 Failed to load resource: the server responded with a status of 404 ().

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>  there was an error in my code for that line.

    IT IS WORKING  YEAH!  Many thanks for your help Xudong

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 16, 2021 5:16 AM