2011-03-25 55 views
3

我有這樣的:格式化TextboxFor內容

@Html.TextBoxFor(m => m.MyClass.MyValue, new { style = "width: 138px", id = "mTextBox", maxlength = 10 }) 

我想格式化的內容。我在哪裏可以做到這一點?

感謝,

回答

3

可以使用

Html.TextBox("MyValue",String.Format('<your format>',Model.MyClass.MyValue)) 

例如 「{0:d}」 或 「{0:C}」 或者如果你希望只使用Html.TextBoxFor則u可以從模型中格式化您的內容。

+0

如何把文化考慮? – MushyPeas 2016-12-30 03:16:59

0

例如,以顯示ShortDate +短時(「3/9/2010 4:15 PM」)

Html.TextBoxFor(model => model.MyClass.TimeStamp, String.Format("{0:g}",Model.MyClass.TimeStamp)) 
3

我用EditorFor的情況下,如本和裝飾我的財產與時間戳DisplayFormat屬性

在我看來:

<%: Html.EditorFor(model => model.StartDate)%> 

,並在我的視圖模型

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
public DateTime StartDate { get; set; } 
1

你也可以做到這一點,只設置格式字符串: TimeSpan Format Strings

@Html.TextBoxFor(model => model.Fecha, "{0:G}", new { @class="vld-required" }) 
3

這個工作對我..

@Html.TextBoxFor(model => model.INVOICE_DATE,"{0:MM/dd/yyyy}", new { maxlength = 10, style = "min-height:30px; min-height: 30px; border-color: green; " }) 
+0

要爲此答案增加價值,請解釋發生了什麼變化,以及它爲什麼起作用。 – miken32 2015-12-04 01:10:46