How to pass parameters or query strings to the top navigation in _Layout.cshtml in ASP.NET
P粉343408929
2023-09-01 10:01:28
<p>In my controller I have three parameters. (GET:/Class/List)</p>
<pre class="brush:php;toolbar:false;">public class ClassController : Controller {
public ActionResult List(string classCode = null, string className = null, List<string> semester = null)
{ ... }
}</pre>
<p>And I got this in my navbar...</p>
<pre class="brush:php;toolbar:false;"><a class="nav-link text-dark" asp-area="" asp-controller="Class" asp-action ="List">Classes</a></pre>
<p>I want to pass a value of parameter semester so that the link looks like <code>localhost/Class/List?semester=9&semester=1</code>. Thanks! </p>
<p>I tried ViewBag and asp-route-id, but failed. </p>
This may not work because your ActionResult List expects a list of strings. In my experience, a list of strings usually requires you to loop through Model -> item.semester to list all the values in the view.
You can try changing
List<string>
to a singlestring
.Then append this to the "a" tag. Let's say you populate a
Viewbag.semesterId
in your controller.You can try to convert List to query string. operate:
A.cshtml:
result: