3

How to disabled text are in mvc. I am using mvc 4.0. below is my code, I want to make conditionally means if m.Changes values length >0 then it should be disable,else it is enabled. how that be possible?

@Html.TextAreaFor(m => m.Changes , new { style = " width:97%; height:50px;", tabindex = 5 }) 
skiskd
  • 423
  • 2
  • 9
  • 20
  • 1
    You can conditionally disable the control as per [this answer](http://stackoverflow.com/questions/33892728/asp-net-mvc-inserts-string-inside-new-object-parameter/33892904#33892904), but you do realize disabled controls do not post back a value, so model binding will fail? –  Feb 08 '16 at 11:31

2 Answers2

5

Conditional disabling is only possible by using JavaScript. First, you need to set an id:

@Html.TextAreaFor(m => m.Changes , new { style = " width:97%; height:50px;", tabindex = 5, id = "someId" }) 

And assuming you use jQuery:

$('#someId').on('keyUp', function() {
    var text = $('#someId').val();
    if (text.length > 0)
      $('#someId').attr('disabled','disabled');
});

I'd like to note, however, that disabling some textbox when the length of it's content is greater than zero, that is, it has any content, just makes no sense, since disabling it will prevent you from modifying it ever again (unless there is some logic that programmatically re-enables it based on some condition).

If your goal is to disable the control if the length of the value on the server is greater than zero (that is, the Changes property has a not null and non-empty value inside the model), then you could do this:

@if (Model.Changes.Length > 0) {
   @Html.TextAreaFor(m => m.Changes , new { style = " width:97%; height:50px;", tabindex = 5, id = "someId", disabled = "disabled" }) 
} else {
   @Html.TextAreaFor(m => m.Changes , new { style = " width:97%; height:50px;", tabindex = 5, id = "someId" }) 
}

If you need to disable it based on a client-side condition, then you can't avoid writing a scipt to do that.

Balázs
  • 2,929
  • 2
  • 19
  • 34
  • @balazs- is there any way to do it without writing script for it.only with property like I have set as style="width:97%" like that... – skiskd Feb 08 '16 at 11:27
1

It is not trivial, because the presence of the disabled attribute will already disable the element, whether or not the attribute actually has a value.

So you can't do something like this:

new { disabled = Model.Changes.Length == 0 } 

You could write an extension method that supports this, as shown in Set disable attribute based on a condition for Html.TextBoxFor. This extension method has a bool disabled parameter, which when true will insert the proper disabled="disabled" attribute.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • You can just wrap the helper inside an `if..else` block and include or omit the disabled property of the `htmlAttributes` object accordingly. If you have to do such a thing at many places, then your solution is indeed better. – Balázs Feb 08 '16 at 11:34
  • Yeah you don't want to copy-paste your `Html.TextAreaFor()`, that's a maintenance hell. – CodeCaster Feb 08 '16 at 11:34