Can I set a read-only text field when using Html.TextBoxFor?

I have the following tag with an Html.TextBoxFor expression and I want the content to be read-only, is this possible?

<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action)%> 
+48
html asp.net-mvc
Mar 29 '10 at 15:20
source share
14 answers

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) %> 
+33
Mar 29 '10 at 15:25
source share
 <%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @readonly = true })%> 
+99
Mar 29 '10 at 15:24
source share

Use the following:

  @Html.TextBoxFor(m => m.Whatever, new {@readonly = "readonly"}) 

If you want to assign a class to it, you can do it the same way by adding the @ class = "" property. Hope this helps :)

+25
Aug 15 '14 at 12:03 on
source share

Do it read only

 @Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @readonly="true"}) 

To disable

 @Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @disabled="true"}) 
+8
Apr 30 '14 at 19:02
source share
 <%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new {readonly=true})%> 
+7
Mar 29
source share
 <%: Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @autocomplete = "off", @readonly=true})%> 

Here's how you set a few properties

+4
Jul 13 2018-11-11T00:
source share

This work is for me ...

 @Html.TextBoxFor(model => model.RIF, new { value = @Model.RIF, @readonly = "readonly" }) 
+3
Oct 30 '11 at 21:50
source share

The following snippet worked for me.

 @Html.TextBoxFor(m => m.Crown, new { id = "", @style = "padding-left:5px", @readonly = "true" }) 
+3
Aug 02 '15 at 17:59
source share

Using the @Hunter example, in the new part {..} add readonly = true, I think this will work.

+1
Mar 29 '10 at 15:24
source share

Another possibility:

 <%= Html.TextBoxFor(model => Model.SomeFieldName, new Dictionary<string, object>(){{"disabled", "true"}}) %> 
+1
Apr 25 '14 at 11:45
source share

In fact, Brain Mains answer is almost right.

 @Html.TextBoxFor(model => model.RIF, new { value = @Model.RIF, @readonly = true }) 
0
Sep 25 '13 at 13:59 on
source share

If you need to apply your own class, you can use

  @Html.TextBoxFor(m => m.Birthday, new Dictionary<string, object>() { {"readonly", "true"}, {"class", "commonField"} } ) 

Where

commonField is a CSS class

and Birthday can be a string field that you can probably use to save the jQuery Datapicker date :)

 <script> $(function () { $("#Birthday").datepicker({ }); }); </script> 

This is an example of real life.

0
09 Oct '13 at 1:03
source share

Setting the readonly attribute to both true and false will not work in most browsers, I did this as shown below, when the page mode is "reloaded", I did not enable the "readonly" attribute.

 @if(Model.Mode.Equals("edit")){ @Html.TextAreaFor(model => Model.Content.Data, new { id = "modEditor", @readonly = moduleEditModel.Content.ReadOnly, @style = "width:99%; height:360px;" }) } @if (Model.Mode.Equals("reload")){ @Html.TextAreaFor(model => Model.Content.Data, new { id = "modEditor", @style = "width:99%; height:360px;" })} 
0
Feb 20 '14 at 23:05
source share
  @Html.TextBoxFor(model => model.IdUsuario, new { @Value = "" + User.Identity.GetUserId() + "", @disabled = "true" }) @disabled = "true" 

To work well

0
Mar 13 '16 at 18:20
source share



All Articles