控制器中的ASP.NET MVC 5双精度值始终为0.0 [英] ASP.NET MVC 5 double value always 0.0 in controller

查看:104
本文介绍了控制器中的ASP.NET MVC 5双精度值始终为0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC 5应用程序中,我有一个表格,用户可以在其中输入价格。当然,该值并不总是整数,因此我在模型中将其定义为double。并且当用户输入一些值时,例如10000.00,然后按提交,发送给控制器的模型的房地产价格始终为0.0。任何想法为什么?

In my ASP.NET MVC 5 app I have form where user can enter price. Of course that value isn't always integer so I defined it as double in my Model. And when User enter some value, e.g. 10000.00 and press submit, model sent to controller always have 0.0 value for property price. Any ideas why ?

我的财产在模型中定义为:

My property is defined in model as:

        [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "_errorEmpty")]
        [Display(Name = "_carPrice", ResourceType = typeof(Resources))]
        public Decimal Cijena { get; set; }

在我看来,我是这样使用的:

In my view I'm using it like this:

           <div class="form-group">
                @Html.LabelFor(model => model.Cijena, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Cijena, new { @class = "form-control", id = "cijena" })
                    @Html.ValidationMessageFor(model => model.Cijena)
                </div>
            </div>

我的控制器:

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult AddNewCar([Bind(Include = "Model, GodinaProizvodnje, GodinaKupnje, DrugiID, Cijena, RegOznaka, RegOd, RegDo, SelectedCityId")] AddCar car)
    {
    .
    .
    .
    }

当模型从视图传递到控制器时,价格始终为提交值0。我尝试将属性定义为Decimal,Double,Float ...始终为0 http://prntscr.com/4hel3f

And when model is passed from view to controller on submit value for Price is always 0. I have tried defined property as Decimal, Double, Float ... always 0 http://prntscr.com/4hel3f

推荐答案

我想您不是在以美国文化运行应用程序。

I guess you're not running your app in en-us culture.

尝试将线程区域性更改为en-us,以便double.Parse认为的十进制分隔符为。

Try changing the thread culture to en-us so that decimal separator considered by double.Parse is "." instead of ",".

<configuration>
<system.web>
    <globalization
       enableClientBasedCulture="true"        
       culture="en-GB"/>
</system.web>

您也可以创建自定义双面/浮动粘合剂。

Alternatively you can create a custom binder for double/float.

这篇关于控制器中的ASP.NET MVC 5双精度值始终为0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆