1

I want the disabled attribute be added to a textbox based on a condition

 @Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "@disabled" : ""})
david
  • 57
  • 2
  • 8

2 Answers2

1

Use

@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )

Note also if you need to add multiple attributes, then it needs to be in the format (where the attributes are cast to object

@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? (object)new { @disabled = "disabled", @class="form-control" } : (object)new { @class="form-control" })

If you have multiple textboxes that use the same sets of attributes, you can assign these to variables in the view

@{
  object number = new { @type = "number", @class="form-control" };
  object disabledNumber = new { @disabled = "disabled", @class="form-control" };
}

and in the form

@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? disabledNumber : number)
@Html.TextBoxFor(m => m.anotherProperty, AnotherCondition ? disabledNumber : number)
.....
  • is there an option to do the condition inside the new{}, because i have a lot of properties in my code – david Dec 10 '14 at 09:55
  • Short answer NO. But what difference does it make - its still the same amount of code –  Dec 10 '14 at 10:02
  • this is the full line how it will stay the same amount of code , i delete some @ because the site dont let me to write it @Html.TextBoxFor(m => m.kidsNumber, new { @class = "textBox1", type = "number", min = "1", max = "30", id = "kidsNumber", name = "kidsNumber", disabled = (Model.kidsDropDown == "2" ? "" : "") }) – david Dec 10 '14 at 10:51
  • Some of those attributes are pointless. Both `id` and `name` are added by the helper and you should NEVER try to override the name (not that it would have worked anyway). You should use the `[Range]` attribute for the `min` and `max` values which means you also get client side validation –  Dec 10 '14 at 11:17
  • And since disabled fields don't post back, there is no point rendering any attributes except `disabled="disabled"` if the condition is `true` so your above example becomes `@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? (object)new { @disabled="disabled" } : (object)new { @class="textBox1", type="number" })` which is actually similar to your non-working code :) –  Dec 10 '14 at 11:28
  • but i will need to write twice @class = "textBox1" because i want to keep the style, Although it disabled – david Dec 10 '14 at 11:31
  • No you don't if you know how to use css selectors correctly. I don't know why you expect to be able to do the impossible but I'll edit my answer to show you how you can do it even less code if you have multiple textboxes. –  Dec 10 '14 at 11:40
-1

You have not said which attribute you want to set ::

you code is without attribute and you should use use readonly instead because disabled fields are not posted on form submit

@Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "@disabled" : ""})

For Disabled attribute Use::

@Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )

For readonly Use something like::

@Html.TextBoxFor(m => m.kidsNumber, new { @readonly= (Model.kidsDropDown != "2" ? "readonly" : "")})
Rahul
  • 2,309
  • 6
  • 33
  • 60
  • That will add the `disabled` (or `readonly`) attribute whatever the value of `Model.kidsDropDown` is - `` and `` will make the textbox disabled. –  Dec 10 '14 at 02:18
  • You copied my answer to correctly add the `disabled` but didn't do it for the `readonly` attribute! –  Dec 10 '14 at 02:24