Can I set text box to readonly when using Html.TextBoxFor?
.NetHtmlasp.net Mvc.Net Problem Overview
I have the following
<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action)%>
.Net Solutions
Solution 1 - .Net
<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @readonly = true })%>
Solution 2 - .Net
Use the following:
@Html.TextBoxFor(m => m.Whatever, new {@readonly = "readonly"})
If you want to assign a class to it you could do it the same way , by adding the @class = "" property. Hope this helps :)
Solution 3 - .Net
Updated for modern versions of .NET per @1c1cle's suggestion in a comment:
<%= Html.TextBoxFor(model => Model.SomeFieldName, new {{"readonly", "true"}}) %>
Do realize that this is not a "secure" way to do this as somebody can inject javascript to change this.
Something to be aware of is that if you set that readonly
value to false
, you actually won't see any change in behavior! So if you need to drive this based on a variable, you cannot simply plug that variable in there. Instead you need to use conditional logic to simply not pass that readonly
attribute in.
Here is an untested suggestion for how to do this (if there's a problem with this, you can always do an if/else):
<%= Html.TextBoxFor(model => Model.SomeFieldName, shouldBeReadOnlyBoolean ? new {{"readonly", "true"}} : null) %>
Solution 4 - .Net
To make it read only
@Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @readonly="true"})
To diable
@Html.TextBoxFor(m=> m.Total, new {@class ="form-control", @disabled="true"})
Solution 5 - .Net
<%= Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new {readonly=true})%>
Solution 6 - .Net
The following snippet worked for me.
@Html.TextBoxFor(m => m.Crown, new { id = "", @style = "padding-left:5px", @readonly = "true" })
Solution 7 - .Net
This work for me...
@Html.TextBoxFor(model => model.RIF, new { value = @Model.RIF, @readonly = "readonly" })
Solution 8 - .Net
<%: Html.TextBoxFor(m => Model.Events.Subscribed[i].Action, new { @autocomplete = "off", @readonly=true})%>
This is how you set multiple properties
Solution 9 - .Net
An other possibility :
<%= Html.TextBoxFor(model => Model.SomeFieldName, new Dictionary<string, object>(){{"disabled", "true"}}) %>
Solution 10 - .Net
Using the example of @Hunter, in the new { .. } part, add readonly = true, I think that will work.
Solution 11 - .Net
In fact the answer of Brain Mains is almost correct:
@Html.TextBoxFor(model => model.RIF, new { value = @Model.RIF, @readonly = true })
Solution 12 - .Net
In case if you have to apply your custom class you can use
@Html.TextBoxFor(m => m.Birthday, new Dictionary<string, object>() { {"readonly", "true"}, {"class", "commonField"} } )
Where are
commonField is CSS class
and Birthday could be string field that you probably can use to keep jQuery Datapicker
date :)
<script>
$(function () {
$("#Birthday").datepicker({
});
});
</script>
That's a real life example.
Solution 13 - .Net
@Html.TextBoxFor(model => model.IdUsuario, new { @Value = "" + User.Identity.GetUserId() + "", @disabled = "true" })
@disabled = "true"
Work very well
Solution 14 - .Net
You can also disabled TextBoxFor
@Html.TextBoxFor(x=>x.day, null, new { @class = "form-control success" ,@disabled="disabled" })
Solution 15 - .Net
By setting readonly attribute to either true or false is not going to work in most browsers, I have done it as below, when the mode of the page is "reload", I've not included "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;" })}