I have a bootstrap menu in a c# web application. The menu looks like this:
<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-controller="Home" asp-action="Index">realestate</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 flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Newquestion" asp-action="Index">Add New Questions</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Chapter
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" asp-controller="Question" asp-action="Index">Salesperson questions - Chapter 1</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
I have a controller called QuestionController.cs which gets control when the user clicks on dropdown and selects the menu option for "Salesperson questions - Chapter 1". This all works perfectly fine.
If I want to add another menu option, say "Salesperson questions - Chapter 2", can I use the same controller? I would want to pass a piece of data to the controller so the controller knows which menu option was selected. I am looking to do something like this:
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" asp-controller="Question" asp-action="Index(0)">Salesperson questions - Chapter 1</a>
<a class="dropdown-item" asp-controller="Question" asp-action="Index(1)">Salesperson questions - Chapter 2</a>
</div>
So in the above code, if the user selected the Chapter 1 option, a value of 0 would be passed to the controller. If the user selected the Chapter 2 option, a value of 1 would be passed to the controller.
Is this possible?
CodePudding user response:
try like this:
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" asp-controller="Question" asp-action="Index" asp-route-id="0">Salesperson questions - Chapter 1</a>
<a class="dropdown-item" asp-controller="Question" asp-action="Index" asp-route-id="1">Salesperson questions - Chapter 2</a>
</div>
Catch the id in your action like this:
public IActionResult MyAction(int? id)
{
//do something with that id
}
You can put whatever instead of id. but you have to catch with that variable name in that action. Suppose you can use asp-route-name="john". than you have to catch name in that action. if you have matching route for name than the url would be like this
controller/action/john
but if it does not match the route defined in the startup then it would wrapped into query parameter like this
controller/action?name=john
In case of query param you have to catch your variable like this
public IActionResult YourAction([FromQuery(Name = "name")] string name)
{
// do whatever with that name
}
for more details https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-6.0#asp-route-value
CodePudding user response:
try this
<a class="dropdown-item" asp-controller="Question" asp-action="Index" asp-route-id="1"> Salesperson questions - Chapter 1 </a>
<a class="dropdown-item" asp-controller="Question" asp-action="Index" asp-route-id="2"> Salesperson questions - Chapter 2 </a>
CodePudding user response:
I believe this is what you are looking for, passing route value
<a asp-controller="Employee"
asp-action="Index"
asp-route-id="@Model.Id">EmployeeId: @Model.Id</a>
Or passing static value to action
<a asp-controller="Question"
asp-action="Index"
asp-route-id="1">Menu Item 1</a>