c# - 尝试将 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 不会有任何数据要获取)。这会是个问题吗?
解决方案
推荐阅读
- ios - RxSwift:数据库事件和不同的调度问题
- amazon-web-services - 不确定为什么上传到 S3 的视频是 0 位
- excel - 有人可以查看我的 Excel 错误并提供有关代码的反馈吗?
- c# - 从另一个视图访问 _Layout 中的按钮
- vue.js - 我可以在 vue.js 中实现 has() 吗?
- java - 导入junit失败
- r - 使用 rbind 组合数据框 + 添加一个单独的列,其中包含各个数据框的名称
- typescript - 可以在 TypeScript 中将元组转换为数组吗?
- elixir - 如何在 Elixir 的地图列表中总结不同键的值
- ansible - Ansible 同步模块错误