9

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";
}

<h2>Create</h2>
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

<fieldset>
    <legend>GpsCoordinateViewModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Latitude)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Latitude)
        @Html.ValidationMessageFor(model => model.Latitude)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Longitude)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Longitude)
        @Html.ValidationMessageFor(model => model.Longitude)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@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:

<system.web>
<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

<!--
    Enabling request validation in view pages would cause validation to occur
    after the input has already been processed by the controller. By default
    MVC performs request validation before a controller processes the input.
    To change this behavior apply the ValidateInputAttribute to a
    controller or action.
-->
<pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=number">
  <controls>
    <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=number" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>
<globalization culture="en-US" uiCulture="en-US" />

Community
  • 1
  • 1
antedesk
  • 756
  • 6
  • 15
  • 30
  • Are you saying that if you enter values with a decimal point, the values are then received as whole number in your contrller? – von v. Apr 17 '13 at 11:31
  • Hi von v. yes, exactly – antedesk Apr 17 '13 at 11:35
  • That's interesting. Even without those dataannotation formatting it should work. I don't even think culture info has something to do with this as you do not get a separator at all! Hmm... – von v. Apr 17 '13 at 11:41
  • @vonv. for instance cultureinfo for French(Belgium) (fr-BE) a valid number is (123.456.789,00) here . is for digit grouping and , is decimal symbol. – shakib Apr 17 '13 at 11:50
  • That's exactly what I meant. Is there a country whose decimal symbol is "empty"? – von v. Apr 17 '13 at 11:51
  • Yes you are right, even if with the fr-BE culture "41.213321" should be converted to C# double 41213321.0 – shakib Apr 17 '13 at 11:57

3 Answers3

7

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

<system.web>
    <globalization culture="en-US" uiCulture="en-US" />
</system.web>

hope this helps

shakib
  • 5,449
  • 2
  • 30
  • 39
  • it works but not so good. The form's fields let me insert short number(only 3 numbers afeter , ) and then when I create a new DbGeography (See System.Data.Spatial) object I have a general exception. – antedesk Apr 17 '13 at 13:30
  • Are you using "en-US"? if using, does 41.213321 not binding correctly? – shakib Apr 17 '13 at 13:40
  • I copy and paste in my system.web tag. If i use the , (eg: 41,213321) the form show me this: "The field Latitude must be a number". If i use 41.213321, the break point show me 41213321.00. In both case the application go in catch and it doesn't insert a new GpsCoordinate. now i edit my question to show you my – antedesk Apr 17 '13 at 13:55
  • Have you used any DataAnnotations on 'Latitute', 'Latitute' properties on 'GpsCoordinateViewModel' class? – shakib Apr 17 '13 at 14:15
  • as you can see up, no i didn't – antedesk Apr 17 '13 at 14:22
0

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

-1

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

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