我有一个查询字符串,我想将其传递到我的 razor View 中。 example.com/25?Type=1&Assigned=1
我想根据查询字符串是否包含某些值来更改我的 html。我试过做
@if (Request.QueryString["Type=1&Assigned=1"])
{
<button id="type1" class="btn-t btn-success type-button"
onclick="SetType(1);"><i class="fa fa-info-circle fa-fw"></i>Invitation</button>
}
else {
<button id="type1" class="btn-t btn-xxxx
type-button" onclick="SetType(1);"><i class="fa fa-info-circle fa-fw">
</i>Invitation</button>
}
但它只显示了else。我做错了什么?
最佳答案
Request.QueryString
甚至不是 boolean
值,所以不明白你在干什么。它是 NameValueCollection
类型。如果你仍然想检查它,你应该这样写:
@if (Request.QueryString["Type"] == "1"
&& Request.QueryString["Assigned"] == "1")
{
...
}
但在 MVC 中使用 QueryString
进行操作确实是不好的做法。使用强类型 View ,不要弄乱Request
本身。
关于c# - 显示某些基于查询字符串的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385435/