我有以下
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>
所以它看起来像这样
它在数据库中保存为3000.00,这是预期的,但是当我返回视图进行编辑时,文本框中的值为3000.0000
我在这里尝试了一些解决方案
Remove trailing zeros of decimal
我认为我遇到的问题是视图上的字段是十进制类型而不是字符串,因此我不确定如何格式化此十进制以删除尾随零,因此它看起来像上面的图片
答案 0 :(得分:0)
您需要使用接受格式字符串
的TextBoxFor
的过载
@Html.TextBoxFor(m => m.Price, "{0:0.00}", new { @class = "form-control", type = "text", id = "txtPrice", onkeypress = "return isNumberKey(event)"})
附注:
type="text"
。 html助手已经为你添加了这个(添加
你有没有理由不使用渲染的默认id
助手,这将是id="Price"
?)。$('#txtPrice').keypress(...