Updated for modern versions of .NET per @ 1c1cle in a comment:
<%= Html.TextBoxFor(model => Model.SomeFieldName, new {{"readonly", "true"}}) %>
Understand that this is not a โsafeโ way to do this, as someone might insert javascript to change this.
Something you need to know is that if you set readonly to false , you will not actually see any changes in behavior! Therefore, if you need to manage this based on a variable, you cannot just connect this variable there. Instead, you need to use conditional logic to just not pass this readonly attribute.
Here is an unverified suggestion on how to do this (if there is a problem with this, you can always do if / else):
<%= Html.TextBoxFor(model => Model.SomeFieldName, shouldBeReadOnlyBoolean ? new {{"readonly", "true"}} : null) %>
Jaxidian Mar 29 '10 at 15:25 2010-03-29 15:25
source share