ReadOnly 属性在 ASP.NET MVC 模型中不起作用 [英] ReadOnly attribute doesn't work in ASP.NET MVC Models

查看:27
本文介绍了ReadOnly 属性在 ASP.NET MVC 模型中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模型类中将一个属性标记为只读,如下所示:

I have marked a property as readonly in the model class, like this:

public class RegisterModel
{
    [Display(Name = "User name")]
    [ReadOnly(true)]
    public string UserName { get; set; }
    ...
}

在我看来:

@Html.EditorFor(m => m.UserName)

但是当我运行应用程序时,文本框不是只读的.

but when I run the application, the textbox is not readonly.

我知道我可以在视图中使用 html 属性将其设为只读,但我更希望这可以在模型类本身中完成.

I know I can use html attributes in the view to make it readonly, but I would prefer if this can be done in the model class itself.

可以实现吗?

推荐答案

[更新] 我认为没有 new { @readonly = "readonly" } 是不可能的.Readonly 属性指定该属性绑定的属性是只读的还是读/写的.此处有详细信息.

[Update] I don't think it is possible without new { @readonly = "readonly" }.Readonly property specifies whether the property this attribute is bound to is read-only or read/write. Details Here.

但您可以尝试 自定义助手 或尝试使用 Editable 而不是 Readonly 在模型上并在您的视图中使用元数据属性.

But you could try for Custom Helpers or try Using Editable instead Readonly on the model and use the metadata property in your View.

[Editable(false)]

我猜你已经调查过了ReadOnly(true) 是否适用于 Html.EditorForModel?

还有一篇好文章 odetocode.com

这篇关于ReadOnly 属性在 ASP.NET MVC 模型中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆