本文介绍了如何将小数属性发布到Web API 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将JSON对象发布到Web api 2(使用Angular),如下所示:
I am posting a JSON object to web api 2 (using Angular) as follows:
var request = $http({
method: "POST",
url: "http://localhost:52389/odata/Venues",
data: {Address: "my address", Latitude: 72.17}
});
此消息已发布到Web api操作:
This is being posted to the web api action:
public IHttpActionResult Post(Venue venue)
地点具有以下属性:
public class Venue
{
[Key]
public int VenueId { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public decimal? Latitude { get; set; }
}
如果我从发布的JSON对象中删除了谷歌纵横,则可以正常运行,但是对于谷歌纵横对象,api操作中的会场参数将变为 null
.如何最好地处理这种十进制?
类型?
If I remove Latitude from the JSON object being posted, then this works, but with the Latitude object, the venue parameter in the api action becomes null
. How do I best handle this decimal?
type?
推荐答案
设法通过在小数点后加上引号来使它起作用:
Managed to get this working by putting the decimal in quotes:
var request = $http({
method: "POST",
url: "http://localhost:52389/odata/Venues",
data: {Address: "my address", Latitude: "72.17"}
});
或者如果您有一个角度数字 latNum
,那么 latNum.toString()
将起作用
Or if you have an angular number, latNum
, then latNum.toString()
will work
这篇关于如何将小数属性发布到Web API 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!