Home > other >  Submenu Not Working in .NET 6 Razor Page Web App
Submenu Not Working in .NET 6 Razor Page Web App

Time:03-25

I am testing a simple .NET 6 web app (razor pages only, no MVC), and run into problem with submenu.

TEST ENVIRONMENT:

OS: Windows 11

IDE: Visual Studio 2022

Bootstrap V4

Browser: Chrome

PROBLEM DESCRIPTION:

After creating the web app, I added a dropdown menu item called "Services" with 3 submenu items (see _layout.cshtml file content at the end of this post). Each submenu maps to a razor page.

When I tested the app, the dropdown menu did not work at all, all razor pages were working fine when tested with urls. Previously, I was able to use same method to implement submenu items within a .NET 5 MVC app.

QUESTION:

  1. The submenu implementation followed the example at the bootstrap website. What is wrong in my _layout.cshtml file?

  2. Are there differences in the way of how _layout.cshtml works in .NET 5 and .NET 6?

TEST CODE:

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

    <!-- Bootstrap CSS file -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <!-- Bootstrap Font Icon CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <!-- Font Awesome library -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/RazorSubmenu.styles.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav >
            <div >
                <a  asp-area="" asp-page="/Index">RazorSubmenu</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-page="/Index">Home</a>
                        </li>
                        <li >
                            <a href="#"  data-toggle="dropdown">Services</a>
                            <div >
                                <a  asp-area="" asp-page="/Services/Item1">Item 1</a>
                                <a  asp-area="" asp-page="/Services/Item3">Item 2</a>
                                <a  asp-area="" asp-page="/Services/Item3">Item 3</a>
                            </div>
                        </li>
                        <li >
                            <a  asp-area="" asp-page="/Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div >
        <main role="main" >
            @RenderBody()
        </main>
    </div>

    <footer >
        <div >
            &copy; 2022 - RazorSubmenu - <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>

CodePudding user response:

You add js reference by local file. .NET 5 default Bootstrap version is Bootstrap v4.x, but .NET 6 default version is Bootstrap v5.x.

So you need download Bootstrap version 4.x or just use jsDelivr:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
  • Related