2013-06-28 37 views
0

我正在開發MVC 4,我想要一個只讀文本框。我決定使用@ Html.DisplayFor但我不能讓它顯示在客戶端?@ Html.DisplayFor()不會顯示客戶端?

<div class="editor-label"> 
    @Html.LabelFor(model => model.GoodsModel.SerialNumber) <label id ="VGoodsSerialNumber" style="color: #FF0000;"/> 
</div> 
<div class="editor-field"> 
    @Html.DisplayFor(model => model.GoodsModel.SerialNumber) 
    @*@Html.EditorFor(model => model.GoodsModel.SerialNumber, new { disabled = "disabled", @readonly = "readonly" })*@ 
</div> 

然後當我跑我找不到它?

enter image description here

+0

謝謝,仍然不會將EditorFor呈現爲只讀。 – Pomster

+0

你有模型類型的模板嗎? – YD1m

回答

2

Html.EditorFor沒有一個htmlAttributes參數。您需要使用Html.TextBoxFor如果你想傳遞HTML屬性:

@Html.TextBoxFor(model => model.GoodsModel.SerialNumber, new { @readonly = "readonly" }) 

此外,使用禁用或只讀。當輸入被禁用時,它的值在提交表單時不會被髮布到服務器。只讀輸入將仍然發佈,但其值不能由用戶更改。