我在模型类中将属性标记为只读,如下所示:
public class RegisterModel
{
[Display(Name = "User name")]
[ReadOnly(true)]
public string UserName { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
在我看来:
@Html.EditorFor(m => m.UserName)
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,文本框不是只读的.
我知道我可以在视图中使用html属性使其只读,但我更喜欢这可以在模型类本身中完成.
可以实现吗?
有没有办法在输出上使用扩展方法扩展基本的html助手(TextBoxFor,TextAreaFor等),而不是仅仅重写整个方法?例如,添加...
@Html.TextBoxFor( model => model.Name ).Identity("idName")
我知道我可以使用以下内容实现这一点,已经......
@Html.TextBoxFor( model => model.Name, new { @id = "idName" })
但是,当您必须开始添加大量属性时,管理变得笨拙和令人沮丧.是否有任何方法可以为这些内容添加扩展而无需传递htmlAttributes每一个细节?
ReadOnly属性似乎不在MVC 4中.可编辑(false)属性不能按照我希望的方式工作.
有类似的东西有效吗?
如果没有,那么我如何创建自己的ReadOnly属性,如下所示:
public class aModel
{
[ReadOnly(true)] or just [ReadOnly]
string aProperty {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以这样说:
@Html.TextBoxFor(x=> x.aProperty)
Run Code Online (Sandbox Code Playgroud)
而不是这(它确实有效):
@Html.TextBoxFor(x=> x.aProperty , new { @readonly="readonly"})
Run Code Online (Sandbox Code Playgroud)
或者这(它确实有效,但未提交值):
@Html.TextBoxFor(x=> x.aProperty , new { disabled="disabled"})
Run Code Online (Sandbox Code Playgroud)
http://view.jquerymobile.com/1.3.2/dist/demos/widgets/forms/form-disabled.html
这样的事可能吗? /sf/answers/819185041/
注意:
[可编辑(假)]无效