首页 > 解决方案 > 在 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。我该如何解决?谢谢

标签: c#asp.net-mvcrazor

解决方案


Json(..)返回一个字符串。尝试您的视图,例如:

@if (@Html.Action("Check", "Home") == "true")
{
    ....
}

或者

@if (bool.Parse(@Html.Action("Check", "Home")) == true)
{
    ....
}

推荐阅读