Home > Back-end >  Bootstrap 5 collapse wont revert on second Button click
Bootstrap 5 collapse wont revert on second Button click

Time:11-17

I have a Button which shows a div on Button Click using Boostrap 5 Class "Collapse". It actually shows the div on the first click, but the content wont hide again when the User clicks the Button a second time.

<div >
   
    <button role="button"  data-bs-toggle="collapse" data-bs-target="#rrCollapse" >Hinzufügen</button>
    <div  id="rrCollapse">
        <div >
            <div >

                <h1>...</h1>
             
            </div>
        </div>
    </div>
</div>

This is the Button and the div which collapses after a click

    <link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
    <link href="~/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>

All files I used.

I am working on ASP.net Core 6.0

CodePudding user response:

If you have used the default _Layout, and in the _Layout.cshtml there are the <link> and <scrpit> , you don't need to add these files to the view .

<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>

result:

enter image description here

Update:

In the view try below code:

@{
    Layout = null;
}



<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<div >
   
    <button role="button"  data-bs-toggle="collapse" data-bs-target="#rrCollapse" >Hinzufügen</button>
    <div  id="rrCollapse">
        <div >
            <div >

                <h1>...</h1>
             
            </div>
        </div>
    </div>
</div>

This is my _Layout.cshtml, you can refer it to change your _layout.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - MVCForeignIT</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/MVCForeignIT.styles.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav >
            <div >
                <a  asp-area="" asp-controller="Home" asp-action="Index">MVCForeignIT</a>
                <button  type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span ></span>
                </button>
                <div >
                    <ul >
                        <li >
                            <a  asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li >
                            <a  asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div >
        <main role="main" >
            @RenderBody()
        </main>
    </div>

    <footer >
        <div >
            &copy; 2022 - MVCForeignIT - <a asp-area="" asp-controller="Home" asp-action="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>
  • Related