c# - 在 ASP.NET 控制器 MVC 中返回布尔值以在 if 语句中进行比较
问题描述
是否可以从 ASP.NET 控制器返回一个布尔值。我写了这个:
public class HomeController : Controllewe
{
public ActionResult Check()
{
var check = new CheckAD();
if (check.CheckSecurityWithAD())
{
...
return Json(true);
}
else
{
...
return Json(false);
}
}
}
我想比较 if 语句中的返回值。我尝试过这种方式:
@if (@Html.Action("Check", "Home") == true)
{
....
}
但我得到了错误cannot apply operator '==' to operands of type string and boolean
。我该如何解决?谢谢
解决方案
Json(..)
返回一个字符串。尝试您的视图,例如:
@if (@Html.Action("Check", "Home") == "true")
{
....
}
或者
@if (bool.Parse(@Html.Action("Check", "Home")) == true)
{
....
}
推荐阅读
- android - 使用深色主题时如何使 Android CardView 可见
- javascript - 仅更新对象数组中的一个属性会做出反应
- ssis - 如何使用“关键字'FROM'附近的语法不正确”异常调试复合实体?
- javascript - 在数组映射中反应访问 setState
- sql-server - 使用存储过程将数据类型 varchar 转换为 int 时出错
- javascript - 处理 3 个微服务的竞争条件的正确方法。(Promise.all ?)
- shell - 回显输出未分配给 shell 脚本中的变量
- mysql - 将单个 MariaDB 转换为 Galera 集群
- phpmyadmin - 在 PhpMyAdmin 中创建用户时出现权限表中未找到所选用户的错误
- if-statement - VHDL条件类型生成(FW的味道)