I’m trying to prevent user from using input in some condition but for disabling it if I add disabled
to it, it will disable no matter that its value is true
or false
.
This is my code:
@Html.TextBoxFor(model => model.inputName, new { @disabled = (condition ? "true" : "false") })
In any condition it will be disabled.
2
Answers
Because whether to disabled depend on your
disabled
attribute instead of thedisabled
attribute valueHere is an example
if you don’t want to use Js to control your attribute we can judge that by
if
condition true to create the element withdisabled
otherwise not.otherwise we can create an extension method for that.
Then we can use like
As I said in comment to this question you can add variable:
Then you can add it in your helper: