asp.net mvc 3 中 DataAnnotations 的行为是否发生了变化?

发布于 2024-12-05 12:30:33 字数 409 浏览 2 评论 0原文

有一个带有属性的模型,

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

我的视图中

@Html.EditorForModel()

当我调用时,我仍然会得到该属性的标准可编辑文本框,

这是为什么?如果文本框仍然可编辑,那么这个 DataAnnotation 属性的意义何在?

Brad Wilson 的帖子

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美人如玉 2024-12-12 12:30:33

这不是 DataAnnotation 属性。请注意,它位于 System.ComponentModel 命名空间中。数据注释位于System.ComponentModel.DataAnnotations命名空间中。

然而,这是我们可以考虑支持的情况。但您究竟期望发生什么以及为什么要这样?

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?

九厘米的零° 2024-12-12 12:30:33

你可以使用

@Html.TextBoxFor(x=> x.ModelProperty, new { @readonly="readonly"})

you can use

@Html.TextBoxFor(x=> x.ModelProperty, new { @readonly="readonly"})
作死小能手 2024-12-12 12:30:33

根据我对您的问题和对其他答案的评论的理解,您只是想显示 BodyMassIndex,而不是使其可编辑。

如果是这种情况,请使用 @Html.DisplayFor 而不是 @Html.EditorFor

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.

孤凫 2024-12-12 12:30:33

AFAIK ReadOnlyAttribute 用于类的属性。 来自 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.

因此,您可以在类中使用它来防止对属性的修改。 (至少是我赋予该属性的含义)

如果您想要一个只读文本框,请使用类似的内容,

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

只读的@第一个告诉编译器绕过保留字

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

煞人兵器 2024-12-12 12:30:33

这适用于带有 Bootstrap 3 的 Vs2013 C#。

            <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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文