2015-02-08 75 views
0

我有以下問題在視圖中顯示的錢

public decimal? Price {get;set;} 

當我到文本框上輸入視圖3000.00(下面的文本框)

<div class="form-group"> 
    <label class="col-lg-3 control-label no-padding-right">Price</label> 
     <div class="col-lg-5"> 
      @Html.ValidationMessageFor(m => m.Price) 
     <div class="input-group"> 
      <span class="input-group-addon">£</span> 
       @Html.TextBoxFor(m => m.Price, new { @class = "form-control", type = "text", id = "txtPrice", onkeypress = "return isNumberKey(event)" }) 
     </div> 
     </div> 
    <label class="col-lg-4 control-label" style="text-align: left">Decimal format</label> 

所以它看起來像此

enter image description here

它在數據庫中保存爲3000.00預計,但是當我返回到視圖來編輯它在文本框中的值是3000.0000

我已經嘗試了一些解決方案,在這裏

Remove trailing zeros of decimal

我想我的問題是在視場爲十進制類型不是字符串的,所以我對如何,所以它看起來像上面的圖片

+1

使用接受的格式字符串'@ Html.TextBoxFor(M => m.Price, 「{0:0.00}」,新的{....})過載' – 2015-02-08 02:36:32

回答

0

你格式化這個十進制刪除尾隨零不確定需要使用接受的TextBoxFor的過載是個格式字符串

@Html.TextBoxFor(m => m.Price, "{0:0.00}", new { @class = "form-control", type = "text", id = "txtPrice", onkeypress = "return isNumberKey(event)"}) 

旁註:

  1. 刪除type="text"。 HTML幫手已經爲你添加了這個(添加 是否有一個原因,你不只是使用默認的id由 幫手,這將是id="Price"?)。
  2. 使用Unobtrusive Javascript而不是污染您的標記 的行爲 - 例如, $('#txtPrice').keypress(...
+0

正確我應該使用默認ID,我只是用來編寫ID等,所以我需要養成習慣,讓幫手分配ID,我也會改變按鍵的工作方式。再次感謝 – 2015-02-08 02:51:18