Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

html - ASP.NET MVC Form and double fields

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" />

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...