只读属性不会在ASP.NET MVC模型工作 [英] ReadOnly attribute doesn't work in ASP.NET MVC Models

查看:109
本文介绍了只读属性不会在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)

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

我知道我可以使用在视图中的HTML属性,使其只读,但我会preFER这是否可以在模型类本身来完成。

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.

能否实现?

推荐答案

[更新]我不认为这是可能没有新{@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.

但是你可以尝试定制的助手或尝试使用编辑而不是只读在模型上,并在您查看使用元数据属性。

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

我猜你已经看着
<一href=\"http://stackoverflow.com/questions/3510081/does-readonlytrue-work-with-html-editorformodel\">Does只读(真)与Html.EditorForModel?


一个美好的文章<一个href=\"http://odeto$c$c.com/blogs/scott/archive/2012/03/11/complete-guide-to-mass-assignment-in-asp-net-mvc.aspx\"相对=nofollow> odeto code.com

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

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