问题描述
我已经标志着财产的只读在模型类,如下:
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)]
我猜你已经看着
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模型工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!