2014-07-02 43 views
1

我想輸入一個帶小數點的數字到文本框中,但是我收到錯誤You must enter a valid valueMVC 4 TextBoxFor HTML5貨幣

有人可以請示範我如何修改下面的代碼,以確保只有帶小數點和2位小數的真實價格可以輸入到表單中。

還有可能在字段前加一個£(&英鎊)字符,但顯然在發佈後,從提交的值中省略了£符號?

test.cs中

[DisplayName("Amount")] 
public Decimal Amount { get; set; } 

Index.cshtml

@Html.TextBoxFor(m => m.Amount, new { @class = "form-control", type = "number", placeholder = "Amount", required = "required" }) 
+3

的伎倆?如果是這樣,我會保持貨幣符號超出文本框,並使用Bootstrap輸入組http://getbootstrap.com/components/#input-groups-basic – DavidG

+0

您可以嘗試使用Html.EditorFor –

+0

我確實使用Boostrap。 – iggyweb

回答

1

我已經結束了使用下面的代碼是HTML5標準。

test.cs中

[Required] 
[DisplayName("Amount")] 
[Range(0.01, 100000.00)] 
public Decimal Amount { get; set; } 

Index.cshtml

@Html.TextBoxFor(m => m.Amount, new { @class = "form-control", type = "number", step = "0.01", max = "100000.00", placeholder = "Amount", required = "required" }) 

這似乎已經做是否使用引導:-)

0

裝飾用正則表達式的屬性。 此外,我注意到你是想在您查看控制發出該電源線屬性,但它是做它在實體的最佳實踐,所以最終的裝飾可能是這樣的:

[Requiered,RegularExpression(@"^\d+.\d{0,2}$", ErrorMessage = "Amount must be a number with two decimal place")] 
public Decimal Amount { get; set; } 

...在你的看法使用EditorFor

@Html.EditorFor(model => Model.Peso) 

對於貨幣符號,我會去的方式@DavidG表明