4

I'm passing an object from jQuery to a MVC3 Controller via a $.ajax POST. While debugging using Developer Tools, I can see the object I'm assigning to the ajax data property. The object contains properties whose value is null. When I debug the Controller, the properties which were null in the JS Debugger are now "null" (strings).

Why is this? What can I do to prevent this from happening?

C# Object

public class User
{
   public string Name { get; set; }
}

Javascript Object

var user = {
Name: null
}

Controller Method

public JsonResult HelloWorld(User user) { .. some logic .. }

ajax Call

var data = user;
$.ajax({
url: '/Controller/HelloWorld/',
data: data,
type: 'post',
success: ...
error: ...
})
tereško
  • 56,151
  • 24
  • 92
  • 147
Mark Meisel
  • 841
  • 2
  • 12
  • 24
  • 3
    Can you show the relevant code (both js and cs)? – MilkyWayJoe Jun 28 '12 at 19:00
  • We aren't doing anything special. Create a c# object, create a javascript object which matches (leaving some properties as null), then use jQuery ajax to POST that object to a Controller method. The object is coming over with the properties as "null" so the logic within the Controller method doesn't matter. – Mark Meisel Jun 28 '12 at 19:07
  • cool, can you show *how* you're doing that `POST` in JavaScript then? – MilkyWayJoe Jun 28 '12 at 19:10
  • I've updated the question for you. – Mark Meisel Jun 28 '12 at 19:13
  • honest question. Have you tried specifying `contentType: 'application/json; charset=utf-8'`? (not 100% sure if it helps in this case tho) – MilkyWayJoe Jun 28 '12 at 19:17
  • @LeeGunn you *are* missing something. It's `null` in js, but it's `string` in cs (with the value of null) – MilkyWayJoe Jun 28 '12 at 19:18
  • Have a look at the accepted answer in [this](http://stackoverflow.com/questions/8448150/asp-net-mvc-receives-null-as-a-string-instead-of-null) link – MilkyWayJoe Jun 28 '12 at 19:25

1 Answers1

4

Yeap, that's an unfortunate side effect with the default model binder. You could avoid it by either not including null properties in the request at all or by using a JSON request:

$.ajax({
    url: '@Url.Action("HelloWorld", "Controller")',
    data: JSON.stringify({ Name: null }),
    contentType: 'application/json',
    type: 'post',
    success: function (result) {
        // ...
    }
});

Things to notice:

  • contentType: 'application/json'.
  • JSON.stringify around the data parameter in order to convert to a JSON string the request.
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • so it turns out `contentType` helps a little in this case after all – MilkyWayJoe Jun 28 '12 at 19:55
  • Yes it helps because it is now the JSON value provider factory using the JavaScriptDeserializer class which handles the parsing instead of the default model binder. – Darin Dimitrov Jun 28 '12 at 19:56
  • yeah, I suggested that but the OP never replied. As for the `null`-"null" issue, I recommended the approach in a different question but `JSON.stringify` is definitely the way to go, so you don't have to check each individual property... +1 – MilkyWayJoe Jun 28 '12 at 19:59