[英]How do I properly post a decimal value in a Razor View?
I have a POCO model as follows: 我有一个POCO模型,如下所示:
public class Tool
{
[Key]
public int ID { get; set; }
public int QtyOnHand { get; set; }
[DisplayFormat(DataFormatString = "{0:#.####}")]
public decimal SizeUS { get; set; }
public string Location { get; set; }
public int OriginalID { get; set; }
[NotMapped]
public bool Selected { get; set; }
[NotMapped]
public bool Selectable { get; set; }
}
In my Razor View I have: 在我的剃刀视图中,我有:
@Html.EditorFor(model => model.SizeUS)
@Html.ValidationMessageFor(model => model.SizeUS)
The problem that I have is the SizeUS value gets rounded to the 1/100th when posted to the controller. 我遇到的问题是,将SizeUS值发布到控制器后会四舍五入到1/100。 I want to be able to post a value of up to 1/1000th or maybe 1/10000th.
我希望能够发布最多1 / 1000th或1 / 10000th的值。 How would I do so ?
我该怎么做?
It's how the decimal
editor is defined for currency reasons: 出于货币原因,定义
decimal
编辑器的方法如下:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
private object FormattedValue {
get {
if (ViewData.TemplateInfo.FormattedModelValue == ViewData.ModelMetadata.Model) {
return String.Format(System.Globalization.CultureInfo.CurrentCulture,
"{0:0.00}",
ViewData.ModelMetadata.Model);
}
return ViewData.TemplateInfo.FormattedModelValue;
}
}
</script>
This template displays decimal values with 2 digits of precision by default, since most users will use decimal values to represent currency.
默认情况下,此模板显示精度为2位数字的十进制值,因为大多数用户将使用十进制值表示货币。 Note that it only does this if you haven't applied a format string (which is the purpose of the check in the if statement).
请注意,只有在您未应用格式字符串的情况下,它才会这样做(这是if语句中检查的目的)。 <%= Html.Encode(FormattedValue) %>
<%= Html.Encode(FormattedValue)%>
ref: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html 参考: http : //bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html
You can either use double
instead, create your own editor template, or simply write out a HTML input
control. 您可以改用
double
,创建自己的编辑器模板,也可以简单地编写HTML input
控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.