MVC 视图 int 到 boolean

MVC View int to boolean

在我看来,我喜欢基于 36 和 37 的 int 值创建一个布尔值。有没有办法做到这一点?还是我需要创建两个布尔值?我有这个 if 语句,我只是喜欢使用 boolean vs int?

查看

@{ 
   boolean UserTyper = Model.TypeId == 36 ? true : false
}

喜欢做这样的事情吗?

 @{ 
       boolean UserTyper = Model.TypeId == 36 or 37 ? true : false
    }



 @if (UserTyper  == true) 
            {

您可以这样检查内联:

bool userTyper = (Model.TypeId == 36 || Model.TypeId == 37);

或者,如果您有想要检查的预设,您可以这样做:

var checkIds = new List<int>() {36, 37};
bool userTyper = checkIds.Contains(Model.TypeId);

如果你想要一行也可以缩短它:

bool userTyper = new List<int> { 36, 37 }.Contains(Model.TypeId)

这种条件逻辑通常表示某种业务需求,因此不应在视图中完成:

class Model
{
    public bool UserType => TypeId == 36 || TypeId == 37;
}

查看:

@if (Model.UserType) 
{
}