首页 > 解决方案 > 尝试将 double 类型的变量设置为等于解析的坐标时,输入字符串的格式不正确?

问题描述

我正在尝试将经度和纬度变量(来自视图模型)设置为等于从数据库表中获取的经度和纬度值(注意:在数据库表中,经度和纬度变量的类型为“nvarchar(50)”)。但是,在执行该特定代码时,我收到一个 system.formatException 说明“输入字符串的格式不正确”。

视图模型属性:

public double Longitude { get; set; }
public double Latitude { get; set; }

数据库属性:

在此处输入图像描述

我将视图模型属性分配给传递的数据库属性的控制器:

 public JsonResult GetMarkers()
        {
            //Disable lazy loading
            db.Configuration.ProxyCreationEnabled = false;

            //Map marker section
            var DynamicMapMarkers = db.tblNpo.Include(zz => zz.tblVerification).Include(xx => xx.tblNpo_Type);

            //Set map marker values
            foreach (var item in DynamicMapMarkers)
            {
                DonatorUserViewModel MapMarkers = new DonatorUserViewModel();

                MapMarkers.NpoID = item.npo_id;
                MapMarkers.NpoName = item.npo_name;
                MapMarkers.NpoType = item.tblNpo_Type.description;
                MapMarkers.VerificationStatus = item.tblVerification.description;
                MapMarkers.Longitude = double.Parse(item.longitude); //this is where the error is thrown
                MapMarkers.Latitude = double.Parse(item.latitude);
                MapMarkerList.Add(MapMarkers);
            }

            var json = MapMarkerList;
            return Json(json, JsonRequestBehavior.AllowGet);
        }

此外。item.longitude 和 item.latitude 的值有可能会一无所获(tblNpo 不会有任何数据要获取)。这会是个问题吗?

标签: c#sqlasp.netasp.net-mvclambda

解决方案


推荐阅读