1

I have some Json that I'm posting back using ajax; the object is build in JavaScript on the client side and received on the MVC side by a C# object with the same property names. I had a mixture of nullable and non-nullable fields and everything was working fine. While making a modification I changed a non-nullable long property to nullable since it won't be set in all situations.

JavaScript

var dataToSend = {
    TestId1: 1,
    TestId2: 2,
    TestId3: 3,
    Action: 'add'
};

$.ajax({
    url: '/testRequestUrl/SaveTestRequest',
    type: 'POST',
    contentType: "application/json",
    data: JSON.stringify(dataToSend)
}).done(function(returnData) {
    alert(returnData);
});

MVC

public class TestRequest
{
    public int? TestId1 { get; set; }
    public int? TestId2 { get; set; }
    public long? TestId3 { get; set; }
    public PostAction Action { get; set; }
}

[HttpPost]
public ActionResult SaveTestRequest(TestRequest request)
{
    // Do some processing and return the result
    var result = DoSomeProcessing(request);
    return Json(result);
}

Originally TestId3 was not nullable and everything worked; once I changed TestId3 to nullable only that field stopped loading the data from the client. I've checked my JSON and TestId3 is getting its value set on the client side but just not making it to the server side. As you can see I have other nullable fields and they are getting their data set just fine so I'm wondering why only the changed field isn't working.

Thanks in advance for any help.

1 Answers1

0

This is fixed by putting the data that will be assigned to a nullable long in quotes in the json being passed to the server side.