user5843610
user5843610

Reputation:

MVC View int to boolean

In my view I like to create a boolean base on the int values of 36 and 37. Is there a way to do this? or do I need to create two boolean? I have this if statement that I just like to use boolean vs int?

View

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

Like to do something like this?

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



 @if (UserTyper  == true) 
            {

Upvotes: 0

Views: 369

Answers (2)

ASh
ASh

Reputation: 35693

This kind of conditional logic usually signifies some kind of business requirement and therefore should not be done in the View:

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

View:

@if (Model.UserType) 
{
}

Upvotes: 0

Lee Stevens
Lee Stevens

Reputation: 482

You can check inline like this:

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

Or if you have a preset you want to check you could do this:

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

You can also shorten it if you wanted to one line:

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

Upvotes: 1

Related Questions