I need to disable an input dynamically when its value is equal to "*". How can I achieve this using the MVC Razor?
@Html.TextBoxFor(m => m.Digits[0], new { @class = "form-control input-lg label_16", @placeholder =
"1st", (Model.Digits[0] == "*" ? "disabled" : "") })
The above code doesn't compile Is this possible?
Try using ternary operator
@Html.TextBoxFor(m => m.Digits[0], Model.Digits[0] == "*" ? (object)new { @class = "form-control input-lg label_16", @placeholder =
"1st", @disabled = "disabled" } : new { @class = "form-control input-lg label_16", @placeholder =
"1st" })
in the code above, the second parameter of @Html.TextBoxFor
helper method will be based on the value of Model.Digits[0]
. If it's *
then the parameter would include the disabled
attribute
new { @class = "form-control input-lg label_16", @placeholder =
"1st", @disabled = "disabled" }
otherwise
new { @class = "form-control input-lg label_16", @placeholder =
"1st" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With