2011-05-06 17 views
0

我剛開始使用MVC3。我使用的字段是這樣的:我在MVC3中輸入字段的寬度

<div class="editor-field"> 
        @Html.TextBoxFor(model => model.Status.RowKey, new { disabled = "disabled", @readonly = "readonly" }) 
       </div> 

它創建這個HTML

<div class="editor-field"> 
        <input disabled="disabled" id="Status_RowKey" name="Status.RowKey" readonly="readonly" type="text" value="0001" /> 
      </div> 

但是我注意到所有的輸入字段具有相同的寬度大約是160像素。有沒有一種方法可以讓它們更寬,爲什麼他們默認這個。在我的編輯場I類

+0

可能重複[如何設置HTML的寬度。 textboxfor?](http://stackoverflow.com/questions/3575067/how-to-set-width-of-html-textboxfor) – 2012-09-06 12:00:20

回答

2

您可以設置大小屬性:

@Html.TextBoxFor(model => model.Status.RowKey, new { size = 200, disabled = "disabled", @readonly = "readonly" }) 

可能更適合將被設置在CSS中輸入寬度,給文本框類,如:

@Html.TextBoxFor(model => model.Status.RowKey, new { @class = "myClass", disabled = "disabled", @readonly = "readonly" }) 

<style> 
    input.myClass { width = 200px; } 
</style> 
+0

謝謝。有沒有辦法可以直接在第一行設置寬度而不使用類? – JonWylie 2011-05-06 15:37:40

+0

謝謝。我剛纔看到你也告訴過我該怎麼做。很棒 – JonWylie 2011-05-06 15:40:13

1

您可以使用CSS。

例如:

input#StatusRowKey /* matches only the textfield with id StatusRowKey */ 
{ 
    width: 250px; 
} 
input    /* matches all input types */ 
{ 
    width: 200px; 
} 
input.Normal /* matches all input fields with class="Normal" */ 
{ 
    width: 100px; 
} 
1

如果你的字段沒有任何樣式您的瀏覽器將應用其自身的默認寬度他們。您需要添加一個樣式給他們

通過添加含

div.editor-field input { width: 300px; } 

或者通過內聯(不推薦)外部CSS文件的

@Html.TextBoxFor(model => model.Status.RowKey, new { disabled = "disabled", @readonly = "readonly", style="width:300px" })