我希望使用属性将视图模型属性标记为只读,以便视图字段只在渲染视图中读取.应用System.ComponentModel.DataAnnotations.EditableAttribute似乎是我需要的确切属性但它似乎不起作用,即文本框字段仍然是可编辑的.我环顾四周,找不到答案,只找到一些相关的问题.下面应用的可编辑属性在呈现视图时不起作用.
[Display(Name = "Last Name")]
[Editable(false, AllowInitialValue = true)]
public string LastName { get; set; }
Run Code Online (Sandbox Code Playgroud)
我可以使用像这样的视图助手函数来实现只读行为,但我的偏好是在模型属性上使用属性.
@functions {
object getHtmlAttributes()
{
if (@ViewBag.Mode == "Edit")
{
return new {style = "width:100px;background:#ff6;", @readonly = "readonly"};
}
return new { style = "width:100px;" };
}
}
@Html.TextBoxFor(model => model.FirstName, getHtmlAttributes())
Run Code Online (Sandbox Code Playgroud)
其他属性完全正常,包括自定义验证属性.你能告诉我数据注释可编辑属性是否在这个上下文中起作用,应该像上面那样工作还是需要做其他事情?谢谢.
是否有键盘快捷键显示文档选项卡的上下文菜单 - 与文档选项卡右键单击相同?
注意:与shift + F10不同,它会调出主上下文菜单.
谢谢