Home > Software engineering >  Disable attribute for HTML input based on a bool parameter from Controller - ASP.NET MVC
Disable attribute for HTML input based on a bool parameter from Controller - ASP.NET MVC

Time:03-29

Is there a way to make an input visible based on a bool variable?

<input type="button" value="Smth" onclick="...">

I am trying to find an HTML helper for this purpose, however no luck so far.

CodePudding user response:

try this

@if ( your bool condition)
{
<input type="button"   value="Smth" onclick="..."> //visible
}
else
{
<input type="button"  value="Smth" onclick="..."> //none-visible
 
//or

<input type="button" hidden value="Smth" onclick="...">
}

CodePudding user response:

You can achieve your required result to set your input field as disabled by using the ternary conditional operator:

<input type="button" value="Smth" onclick="..." @(myBoolCondition == "true" ? "disabled='disabled'" : "") >

Where myBoolCondition is your variable holding the result of your condition.

  • Related