MarkKGreenway
MarkKGreenway

Reputation: 8764

ASP.NET MVC post serialization not matching data input

Request

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

Response

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}

Controller Action

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

View Model

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

In goes 10100745654968505 and out comes 10100745654968504

Upvotes: 4

Views: 156

Answers (1)

L.B
L.B

Reputation: 116138

It is all related to internal representaion of floating point numbers and precision. See this for ex, http://en.wikipedia.org/wiki/Double-precision_floating-point_format.

Changing doubles to decimals should make your code work.

More detailed explanation from Jon Skeet:https://stackoverflow.com/a/618596/932418

Upvotes: 2

Related Questions