Has the behavior for DataAnnotations in asp.net mvc 3 changed?
Asked Answered
B

5

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

Badger answered 19/9, 2011 at 19:44 Comment(0)
H
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?

Hermaphroditism answered 20/9, 2011 at 0:42 Comment(2)
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.Badger
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?Badger
A
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

Atrioventricular answered 19/9, 2011 at 19:50 Comment(0)
P
0

you can use

@Html.TextBoxFor(x=> x.ModelProperty, new { @readonly="readonly"})
Phaedrus answered 19/9, 2011 at 21:29 Comment(1)
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 ReadOnlyAttributeBadger
F
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.

Furtek answered 20/9, 2011 at 5:32 Comment(0)
W
-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>
Wivestad answered 24/10, 2014 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.