How can I conditionally indicate if HTML / readonly input is disabled OR NOT?

Here is an example of what I'm trying to achieve:

@Html.EditorFor(m => m.Description, new { htmlAttributes = new { @class = "form-control", @readonly = Model.IsReadOnly, disabled = Model.IsDisabled } }) 

The problem is that the browser considers the readonly and disabled tokens without checking their contents, so when the IsReadOnly and IsDisabled false , it will still display as disabled .

Is there any simple solution for this?

0
html c # asp.net-mvc razor disabled-input
Mar 25 '15 at 4:53
source share
1 answer

HTML: -

 @Html.EditorFor(m => m.Description, new { htmlAttributes = new { @class = "form-control" } }); 

Try using jQuery as shown: -

 if(@Json.Encode(Model.IsReadOnly)) { $('#Description').attr('readonly','readonly') } if(@Json.Encode(Model.IsDisabled)) { $('#Description').attr('disabled','disabled') } 
+1
Mar 25 '15 at 5:09
source share



All Articles