2016-10-05 32 views
0

我有一個預先填充的金額字段。當我運行網絡應用程序時,它顯示爲125.4500。我試圖格式化,但我沒有運氣。將文本框從十進制格式設置爲美元金額

這裏是我的PaymentModel:

[Required] 
    [DataType(DataType.Currency)] 
    [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)] 
    [Display(Name = "Payment Amount")] 
    public decimal Amount { get; set; } 

DataFormatString似乎沒有任何影響。

而且我在表格上的代碼:

@Html.TextBoxFor(m => m.Amount, new { @class = "makePaymentTextRight width90" }) 

我嘗試這樣做:

@Html.TextBoxFor(m => string.Format("{0:F2}",m.Amount), new { @class = "makePaymentTextRight width90" }) 

但是,這給了我這個錯誤:

System.InvalidOperationException: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

我如何得到這個格式到2位小數?

+0

時才尊重@ StephenMuecke Wow,很簡單。發佈作爲答案,我會給你信用。 Tyvm! – ErocM

回答

1

DisplayFormatAttribute使用EditorFor()DisplayFor()方法(使用內置模板)

要格式化的值,使用的TextBoxFor()this overload其中第二參數是格式字符串

@Html.TextBoxFor(m => m.Amount, "{0:F2}", new { @class = "makePaymentTextRight width90" })