0

I have a stupid problem here. I'm developing a web application with ASP .NET MVC 4, EF5, and Ext JS (Sencha).

The thing is, by design (I don't know why), Ext JS when doing ajax requests is converting js objects with null values to empty strings.

I'll over simplify the example just for the sake of it.

So if I have a variable in JavaScript

icon_id : null

When I get the JObject server-side it becomes

icon_id : ""

when I pass icon_id as the parameter for the request. So when I write icon_id.ToObject<MyNETIconIDClass>() I get an exception which tells me it can't convert from string to nullable int. Which is understandable.

I may be misunderstanding the logic behind this, but since I don't want to mess around with Ext JS source code, is there any way I could tell the Json.NET deserializer to consider empty strings as null ?

And as far as I've seen, using converters would be troublesome because I would have to change every nullable int property in every class in my model just to make this work right.

Conrad Clark
  • 4,533
  • 5
  • 45
  • 70
  • How are you passing it to the server? Ext won't (or certainly, shouldn't) convert that to any empty string. – Evan Trimboli Oct 05 '12 at 02:05
  • Please check this link: http://www.sencha.com/forum/showthread.php?113391-Ajax-request-is-converting-params-with-null-values-to-empty-strings&s=8531c7a1525c9a044571d6972b51b12f . This seems to be what's happening to me. I try to pass a nullable int in `params`, and when the value turns out to be null I get the exception when I try to deserialize. – Conrad Clark Oct 05 '12 at 02:10
  • 1
    So it's a get request? In that case, Ext isn't doing anything untoward, there's no real way to represent the difference between null & ''. – Evan Trimboli Oct 05 '12 at 02:52
  • Oh my god, I didn't notice that! I replaced `params` with `jsonData` to identify it as a post request and it worked. I can accept your answer if you put your comment as answer ;) – Conrad Clark Oct 05 '12 at 02:57

1 Answers1

0

As per comments, OP was sending a GET request as opposed to a POST.

Evan Trimboli
  • 29,900
  • 6
  • 45
  • 66