2013-04-17 34 views
8

我正在開發一個asp.net mvc門戶來管理使用localDB的GPS座標。我的模型是:ASP.NET MVC窗體和雙字段

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

自動生成的ADN相關視圖中創建一個新的GpsCoordinate是:

@{ 
    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") 
} 

的問題是,當我插入緯度:41.213321和經度:12.123432(例如),使用breakpoin本地值是:Latitude:41213321 Longitude:12123432. 由於位置的準確性,我需要使用double,但是如何?

我也看了這個問題: How should I use EditorFor() in MVC for a currency/money type?

MVC3 - 3 decimal places on type double with leading zero

但解決方案並沒有爲我工作。 有什麼建議嗎?

編輯: 我的webconfig是:

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

+0

你是說如果你輸入一個小數點的數值,那麼這些數值就會作爲整數在你的控制器中被接收到? –

+0

Hi von訴 是的,正好是 – antedesk

+0

這很有趣。即使沒有這些數據註釋格式,它也應該可以工作。我甚至不認爲文化信息與此有關,因爲你根本沒有得到分隔符!嗯... –

回答

5

可能是由CultureInfo的引起,小數點分隔一些文化使用,代替.。嘗試設置web.config中的下面,

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

希望這有助於

+0

它的作品,但不是很好。表單的字段讓我插入短數字(只有3個數字afeter),然後當我創建一個新的DbGeography(請參閱System.Data.Spatial)對象時,我有一個一般的異常。 – antedesk

+0

您使用的是「en-US」嗎?如果使用,41.213321沒有正確綁定? – shakib

+0

我在我的system.web標記中複製並粘貼。如果我使用,(例如:41,213321),表單顯示給我這個:「字段緯度必須是一個數字」。如果我使用41.213321,斷點顯示41213321.00。在這兩種情況下,應用程序都會進入捕捉狀態,並且不會插入新的GpsCoordinate。現在我編輯我的問題向您顯示我的 antedesk

1

使用十進制而不是雙作爲您的數據類型,以及可能的幫助。

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

不起作用... – antedesk

1

那麼,使用該字符串作爲數據類型並將其解析爲稍後從控制器中作爲十進制值。

+0

這是一個骯髒的解決方案:) – antedesk