Asp.net
ASP.NET MVC 表單和雙欄位
我正在開發一個 asp.net mvc 門戶來使用 localDB 管理 GPS 座標。我的模型是:
public class GpsCoordinateViewModel { double Latitute { get; set; } double Longitude { get; set; } }用於創建新 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(例如)時,使用斷點本地值是:緯度:41213321 經度:12123432。由於位置的準確性,我需要使用雙精度,但是如何?
我還閱讀了這個問題: 我應該如何在 MVC 中使用 EditorFor() 作為貨幣/貨幣類型?
但解決方案對我不起作用。有什麼建議嗎?
編輯:我的網路配置是:
<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" />
可能是由文化資訊引起的,某些文化使用
,而不是.小數分隔符。嘗試在 web.config 中設置以下內容,<system.web> <globalization culture="en-US" uiCulture="en-US" /> </system.web>希望這可以幫助
那麼,使用 String 作為 DataType 並稍後從 Controller 將其解析為 Decimal 值。