2015-02-08 73 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