2012-01-06 106 views

回答

195
@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" }) 

歡迎您,使HTML幫手這一點,但是這根本就像任何其他的HTML屬性。你會爲具有其他屬性的文本框製作HTML助手嗎?

+1

@Shyju對不起,我錯過了'readonly'屬性'@'前綴。看我的編輯。 – 2012-01-06 17:36:05

+7

將來,如果您在向動態對象參數添加屬性時遇到任何錯誤,您可以使用「@」作爲前綴。您通常只會看到與HTML屬性匹配的關鍵字(如只讀,類等) – 2012-01-06 17:40:41

+9

@BradChristie:No;你只需要一個'@'來使用匹配** C#**關鍵字的屬性。 – SLaks 2012-01-06 17:55:19

27

更新: 現在,非常簡單的將html屬性添加到默認編輯器模板。意味着不是這樣:

@Html.TextBoxFor(m => m.userCode, new { @readonly="readonly" }) 

你根本可以這樣做:

@Html.EditorFor(m => m.userCode, new { htmlAttributes = new { @readonly="readonly" } }) 

優點:你有沒有呼籲模板.TextBoxFor等。只需撥打.EditorFor即可。


雖然@鯊魚的解決方案正常工作,這是簡單實用,我的解決方案(我總是使用)是這樣的一個創建editor-template,可以把手readonly屬性

  1. 創建文件夾中~/Views/Shared/
  2. 名爲EditorTemplates創建一個名爲剃刀PartialViewString.cshtml
  3. 與此代碼填寫String.cshtml

    @if(ViewData.ModelMetadata.IsReadOnly) { 
        @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, 
         new { @class = "text-box single-line readonly", @readonly = "readonly", disabled = "disabled" })          
    } else { 
        @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, 
         new { @class = "text-box single-line" }) 
    } 
    
  4. 在模型類,把[ReadOnly(true)]屬性上你想成爲readonly性能。

例如,

public class Model { 
    // [your-annotations-here] 
    public string EditablePropertyExample { get; set; } 

    // [your-annotations-here] 
    [ReadOnly(true)] 
    public string ReadOnlyPropertyExample { get; set; } 
} 

現在你可以使用簡單的剃鬚刀的默認語法:

@Html.EditorFor(m => m.EditablePropertyExample) 
@Html.EditorFor(m => m.ReadOnlyPropertyExample) 

第一個,使正常text-box這樣的:

<input class="text-box single-line" id="field-id" name="field-name" /> 

;第二,將呈現給;

<input readonly="readonly" disabled="disabled" class="text-box single-line readonly" id="field-id" name="field-name" /> 

您可以使用此解決方案的任何類型的數據(DateTimeDateTimeOffsetDataType.TextDataType.MultilineText等)。只需創建一個editor-template

+2

+1因爲您使用了」ViewData.ModelMetadata.IsReadOnly「。我期待MVC會考慮到這些東西,在第4版 – cleftheris 2012-10-24 11:00:55

+0

@cleftheris以及我們在5版,現在,MVC還是沒有把他們;) – 2014-07-23 19:59:31

+1

非常有用的 - 謝謝:) – Nildarar 2014-07-24 01:46:33

5

與TextBoxFor該解決方案是確定的,但如果你不希望看到這樣的現場編輯框時尚(這可能是有點困惑的用戶)涉及變更如下:

1剃刀之前代碼改變

<div class="editor-field"> 
    @Html.EditorFor(model => model.Text) 
    @Html.ValidationMessageFor(model => model.Text) 
</div> 

2.更改後

<!-- new div display-field (after div editor-label) --> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.Text) 
</div> 

<div class="editor-field">    
    <!-- change to HiddenFor in existing div editor-field --> 
    @Html.HiddenFor(model => model.Text) 
    @Html.ValidationMessageFor(model => model.Text) 
</div> 

通常這個解決方案禁止提交編輯,但顯示它的價值。 不需要代碼隱藏修改。

+1

我認爲在這裏爲類編輯器字段和顯示字段設置CSS可能會非常有幫助。 – DOK 2014-10-30 18:36:53

1

與學分由@Bronek和@Shimmy

以前的答案這是我做了samething在ASP.NET核心

<input asp-for="DisabledField" disabled="disabled" /> 
<input asp-for="DisabledField" class="hidden" /> 

第一個輸入是隻讀的,第二個通價值的控制器,並hidden.Hope它將是有用的人使用ASP.Net核心。

0
@Html.TextBox("Recivers", Model, new { @class = "form-control", style = "width: 300px", @readonly = "readonly" }) 
0
@Html.TextBoxFor(model => model.IsActive, new { readonly= "readonly" }) 

這是蠻好的文本框中。但是,如果你嘗試做同爲checkbox然後如果你使用它

@Html.CheckBoxFor(model => model.IsActive, new { onclick = "return false" }) 

嘗試使用它。但是不要使用disable因爲禁用總是發送默認值false到服務器或者它在選中或取消選中州。而readonly不適用於複選框和radio buttonreadonly只適用於text字段。