ASP.NET MVC Form and double fields

匿名 (未验证) 提交于 2019-12-03 01:07:01

问题:

I'm developing an asp.net mvc portal to manage GPS coordinates using localDB. My model is:

public class GpsCoordinateViewModel {     double Latitute { get; set; }     double Longitude { get; set; } }

the autogenerated adn related View to create a new GpsCoordinate is:

@{     ViewBag.Title = "Create"; }  

Create

@using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true)
GpsCoordinateViewModel
@Html.LabelFor(model => model.Latitude)
@Html.EditorFor(model => model.Latitude) @Html.ValidationMessageFor(model => model.Latitude)
@Html.LabelFor(model => model.Longitude)
@Html.EditorFor(model => model.Longitude) @Html.ValidationMessageFor(model => model.Longitude)

}

@Html.ActionLink("Back to List", "Index")
@section Scripts { @Scripts.Render("~/bundles/jqueryval") }

the problem is that when I insert as Latitude: 41.213321 and Longitude: 12.123432 (eg), using breakpoin local values are: Latitude: 41213321 Longitude: 12123432. I need to use double because of location's accuracy, but how?

I also read this questions : How should I use EditorFor() in MVC for a currency/money type?

MVC3 - 3 decimal places on type double with leading zero

but the solutions didn't work for me. Any suggestion?

EDIT: My webconfig is:

                     

回答1:

Might be caused by cultureinfo, some culture use , instead of . for decimal separator. try setting the following in web.config,

      

hope this helps



回答2:

Use Decimal Instead of Double as Your Data Type, and That might Help.

public class GpsCoordinateViewModel {     decimal Latitute { get; set; }     decimal Longitude { get; set; } }


回答3:

Well then, Use the String as a DataType and Parse it as a Decimal value Later from the Controller.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!