0

I want to disable a button if this property is false:

public bool HasRightSELECT { get; set; }

I tried to achieve thsi with:

<button type="submit" class="btn btn-info" disabled="!@Model.TablesWithRights[i].HasRightSELECT" name="View" value="@i">View</button>

or

<button type="submit" class="btn btn-success" disabled="@{!Model.TablesWithRights[i].HasRightSELECT}" name="Insert" value="@i">Insert</button>

I know I could just rename the property to HasNORightSelect but I am wondering how I can set the disable value with the "inverted" value.

Liam
  • 27,717
  • 28
  • 128
  • 190
David Walser
  • 183
  • 20
  • I can accept answeres in two minutes and solved it a little bit different. I kind of feel bad for @AlexB if i would answere my own question now. – David Walser Jul 25 '18 at 11:57

2 Answers2

2

If disabled is present, no matter the value, your button will be disabled.

You shoud try something like :

<button type="submit" class="btn btn-info" @(!Model.TablesWithRights[i].HasRightSELECT ? "disabled" : "") name="View" value="@i">
    View
</button>
AlexB
  • 7,302
  • 12
  • 56
  • 74
1

Just add an if statement:

@if(Model.TablesWithRights[i].HasRightSELECT)
{
     <input disabled/>    
}
else
{
     <input/>
}

BTW disabled is a property (not an attribute) so shouldn't contain a value. It's presence designates if something is disabled or not.

Another (cleaner) option would be to add a computed value to your model:

public bool HasRightSELECT { get; set; }
public string HasRightSELECTdisabled
{
   get
   {
       if (this.HasRightSELEC)
           return "disabled";
       else
           return string.Empty;
   }
}

<input @Model.TablesWithRights[i].HasRightSELECTdisabled />
Liam
  • 27,717
  • 28
  • 128
  • 190