2013-02-28 39 views
1

如何使以下字段为只读?HTML帮助程序标记文本框使其只读

<%: Html.TextBoxFor(x => x.Age, new { value = "0"}) %> 
+1

@karthik一个帮手,可问题是, dublicate,但Darin的第二个建议对于这个问题很重要。因为这仅适用于强类型助手 – 2013-02-28 10:11:33

回答

7

您可以设置readonly属性:

<%= Html.TextBoxFor(x => x.Age, new { @readonly = "readonly" }) %> 

如果你想禁用文本框(同为只读用户,但其价值不会被发送到服务器时,形式提交),你可以使用disabled属性:

<%= Html.TextBoxFor(x => x.Age, new { disabled = "disabled" }) %> 

至于设置文本框而言的默认值,我建议你在你的同事这样做ntroller,填充模型时:

MyViewModel model = ... 
model.Age = 0; 
return View(model); 
0

使用以下

<%= Html.TextBoxFor(x => x.Age, new { @readonly = "readonly" }) %> 

您可以在同一时间通过多个属性,同时呼吁这样

<%= Html.TextBoxFor(x => x.Age, new { @readonly = "readonly", @class="Text", style="INLINE STYLE" }) %>