MVC3 EditorFor readOnly的 [英] MVC3 EditorFor readOnly
问题描述
我想在编辑页面,使readOnly的与EditorFor。
I want to make readOnly with EditorFor in edit page.
我试图把只读和残疾人为:
I tried to put readonly and disabled as:
<div class="editor-field">
@Html.EditorFor(model => model.userName, new { disabled = "disabled", @readonly = "readonly" })
</div>
但是,这是行不通的。我怎样才能让禁用编辑这个领域?
However, it does not work. How can I make to disable edit this field?
感谢您。
推荐答案
该EditorFor HTML帮助不具有采取HTML属性重载。在这种情况下,你需要使用一些更具体的像TextBoxFor:
The EditorFor html helper does not have overloads that take HTML attributes. In this case, you need to use something more specific like TextBoxFor:
<div class="editor-field">
@Html.TextBoxFor(model => model.userName, new
{ disabled = "disabled", @readonly = "readonly" })
</div>
您仍然可以使用EditorFor,但你需要有一个自定义EditorTemplate一个TextBoxFor:
You can still use EditorFor, but you will need to have a TextBoxFor in a custom EditorTemplate:
public class MyModel
{
[UIHint("userName")]
public string userName { ;get; set; }
}
然后,在你查看/共享/ EditorTemplates文件夹,创建一个文件userName.cshtml。在该文件中,把这个:
Then, in your Views/Shared/EditorTemplates folder, create a file userName.cshtml. In that file, put this:
@model string
@Html.TextBoxFor(m => m, new { disabled = "disabled", @readonly = "readonly" })
这篇关于MVC3 EditorFor readOnly的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!