c# – ASP.NET MVC后序列化不匹配数据输入

请求

POST http://localhost:51446/Home/AddUserToGroup HTTP/1.1
Host: localhost:51446
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:51446/Home/ViewFriends
Content-Length: 41
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

userId=24401403&groupId=10100745654968505

响应

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 5.0
X-AspNet-Version: 4.0.30319
Date: Sat, 11 Jan 2014 22:54:19 GMT
Content-Length: 47

{"groupId":10100745654968504,"userId":24401403}

控制器动作

public ActionResult AddUserToGroup(AddUserVM vm)
{
    return Json(vm);
}

查看模型

public class AddUserVM
{
    public double groupId { get; set; }
    public double userId { get; set; }
}

在10100745654968505和10100745654968504

最佳答案 它全部与浮点数和精度的内部表示有关.见前,
http://en.wikipedia.org/wiki/Double-precision_floating-point_format.

将双精度值更改为小数应该可以使代码正常工作.

Jon Skeet的更详细解释:https://stackoverflow.com/a/618596/932418

点赞