bootstrap-4 - 从下拉菜单中将参数发送到相同的 url
问题描述
我正在尝试让多个菜单链接访问同一个剃须刀页面,但发送不同的参数以从数据库返回不同的结果。
通常我只会将该值附加到查询字符串并在单击新页面链接时检索它,但我不知道剃须刀页面如何做到这一点。例如,我想转到我的剃须刀页面 CourseList.cshtml,并通过会计作为值,所以我只能拉回会计课程。
asp-page="/CourseList?ccat=accounting"
我知道我可以为每个课程类别制作单独的 CourseList 页面,但这听起来很脏,并且随着新类别的添加将需要更高的维护。
<ul class="navbar-nav flex-grow-1">
<li class="nav-item dropdown">
<a class="nav-link text-dark dropdown-toggle"
href="#" id="navbaddrop" data-toggle="dropdown">
Courses
</a>
<div class="dropdown-menu">
<a class="dropdown-item" asp-page="/CourseList?ccat=accounting">Accounting</a>
<a class="dropdown-item" asp-page="/CourseList?ccat=general">General</a>
<a class="dropdown-item" asp-page="/CourseList?ccat=it">IT</a>
<a class="dropdown-item" asp-page="/CourseList?ccat=manufacturing">Manufacturing</a>
</div>
</li>
</ul>
解决方案
如果您希望该值出现在查询字符串中,请将其传递给asp-route-*
锚标记助手的参数:
<a class="dropdown-item" asp-page="/CourseList" asp-route-ccat="accounting">Accounting</a>
*
替换为参数的名称。
或者,您可以通过在 Razor 页面中将路由数据参数指定为指令的一部分,将其作为路由参数(URL 中的段)传递@page
:
@page "{ccat}"
然后生成的链接将如下所示:
CourseList/accounting
CourseList/general
在页面本身中,您添加一个属性,该BindProperty
属性表示应用了属性的参数:
public class CourseListModel : PageModel
{
[BindProperty(SupportsGet=true)]
public string Ccat { get; set; }
...
模型绑定将自动将查询字符串值分配给公共属性。更多信息:
推荐阅读
- windows - GDI+ 字体回退不适用于表情符号
- django - 呈现一个将列表值显示为复选框值的表单。[列表中的每个条目代表一个复选框字段]
- spring-boot - Spring AMQP 处理 2 ConnectionFactory
- c - 我正在尝试编写一个 C 程序来将文件中的整数存储到数组中,但它不起作用。有人能帮我吗?
- django - 在 django 中上传文件时处理文件
- xpages - Notes XPages typeahead 返回一些带有方括号的名称
- powershell - 如何使用当前日期(yyyy-mm-dd)作为带有 Get_Content 命令的 Select-String -Pattern 的一部分
- c# - 对依赖于 httpclient 响应的服务类进行单元测试
- c++ - 尝试使用 libgit2 快进时“无法在符号引用上设置 OID”
- airflow - 增加气流中特定 DAG 的超时