this is just html, bootstrap css and javascript. no code behind required. you should be to use collapse and collapse toggle, so you wouldn't even need to write the javascript.
you are using the navbar which is for mobile and is a top menu, not left. typically you would use a drop down. you should just use nav. see docs:
https://getbootstrap.com/docs/5.1/components/navs-tabs/
simple left 3 column nav,
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Collapsible Submenu</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<style>
.submenu {
margin-left:10px;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-3">
<ul class="nav flex-column navbar-light bg-light">
<li class="nav-item">
<a class="nav-link" href="#">Main Menu 1</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="collapse" href="#submenu2">Main Menu 2</a>
<ul class="submenu list-unstyled collapse" id="submenu2">
<li><a class="nav-link" href="#">Submenu 2.1</a></li>
<li><a class="nav-link" href="#">Submenu 2.2</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Main Menu 3</a>
</li>
</ul>
</div>
<div class="col-9">
main body
</div>
</div>
</div>
</body>
</html>