4

I have a Model with a property

[ReadOnly(true)]
public decimal BodyMassIndex { get; private set; }

In my View when I call

@Html.EditorForModel()

I still get a standard editable textbox for that property

Why is this? if the textbox is still editable whats the point of this DataAnnotation Attibute?

Brad Wilson's post

davidfowl
  • 37,120
  • 7
  • 93
  • 103
nacho10f
  • 5,816
  • 6
  • 42
  • 73

5 Answers5

4

That is not a DataAnnotation attribute. Notice it's in the System.ComponentModel namespace. Data Annotations are in the System.ComponentModel.DataAnnotations namespace.

However, this is a case where we could consider supporting it. But what exactly did you expect to happen and why do you want this?

Haacked
  • 58,045
  • 14
  • 90
  • 114
  • Well, this example is for a PhysicalTest ViewModel. the form is supposed to display textboxes for Weight and Height. the BodyMassIndex is not supposed to be an input at all but I do want the user to know the BodyMassIndex (which is calculated from weight and height)...... I wanted to just use EditorForModel without having to create a custom EditorTemplate... imagine a form with two input text fields and then a BodyMAssIndex label that changes as soon as any of those two values change. – nacho10f Sep 20 '11 at 04:28
  • judging by your response I can tell Im not using this Attribute properly :s .Would you mind pointing out what the normal usage for this Attribute is? – nacho10f Sep 20 '11 at 04:33
0

AFAIK the ReadOnlyAttribute is for property of class. From MSDN

 Members that are marked with the ReadOnlyAttribute set to true or that do not have a Set method 
 cannot be changed. Members that do not have this attribute or that are marked with the 
 ReadOnlyAttribute set to false are read/write, and they can be changed. The default is No.

So you use this inside your classes to prevent modification to the properties. (at least the meaning I give to that attribute)

If you want a textbox readonly use something like that

 @Html.TextBox("MyText", "my text", new { @readonly="readonly" })

the @ first of readonly tell the compiler to bybass the reserved word

Iridio
  • 9,213
  • 4
  • 49
  • 71
0

you can use

@Html.TextBoxFor(x=> x.ModelProperty, new { @readonly="readonly"})
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • This would work but I want to use EditorForModel and defining a custom EditorTemplate just for this seems overkill.. also, Im still not sure what is the purpose and ordinary usage of the ReadOnlyAttribute – nacho10f Sep 20 '11 at 04:35
0

From what I understand of your question and the comments on other answers, you simply want to display the BodyMassIndex, not have it as editable.

If this is the case, use @Html.DisplayFor rather than @Html.EditorFor.

dnatoli
  • 6,972
  • 9
  • 57
  • 96
-1

This works in Vs2013 C# with Bootstrap 3.

            <div class="form-group">
                @Html.LabelFor(model => model.PasswordHash, htmlAttributes: new { @class = "control-label col-md-3" })
                <div class="col-md-6">
                    @Html.EditorFor(model => model.PasswordHash, new { htmlAttributes = new { @class = "form-control", @readonly="readonly" } })
                    @Html.ValidationMessageFor(model => model.PasswordHash, "", new { @class = "text-danger" })
                </div>
            </div>
Bill
  • 1