3

I have a controller with the following action code:

public JsonResult CheckPasswordStrength(string password, string username, string firstname, string lastname)
{
   ...
   return this.Json(jsonResponse);
}

Then in my javascript on page I call

$.getJSON('http://localhost/site/controller/CheckPasswordStrength', { password: 'test', username: null, firstname: null, lastname: null }, function(data) {
    ...
});

The problem is that the parameters that are null (firstname, lastname, username) in my javascript call, they are not null in invoked controller action. They have string "null" value. I've already tried with undefined values of parameters in my javascript but it doesn't help.

If I specify only parameters that are not null (without null parameters) in my javascript call, the parameters in my action are transferred correctly.

How should I pass null parameters in my $getJSON call that will be correctly transffered to controller action or what should I do on MVC side?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Peter Starbek
  • 703
  • 8
  • 12

2 Answers2

7

I'm assuming your building this object literal from the values of some form fields. Why not post empty strings instead? If you really want to leave values empty, don't pass them at all, strip the nulls out of the data before sending:

function stripNull(obj) {
  for (var i in obj) {
    if (obj[i] === null) delete obj[i];
  }
  return obj;
}

$.getJSON('http://localhost/site/controller/CheckPasswordStrength', 
  stripNull({ password: 'test', username: null, firstname: null, lastname: null }), 
  function(data) {
   //...
});

Although I suggest against reaching into other peoples frameworks and overriding things - you could make the $.param function (which is what jQuery uses to turn your object into a query string) call stripNull automatically

(function($) {
  // function stripNull() { ... }
  var oldParam = $.param;
  $.param = function(obj) { return oldParam.call(this, stripNull(obj)); }
})(jQuery);

If you would prefer to pass an empty string, or some other "null identifier" (say \01 for instance) you can still use stripNull() but change delete obj[i]; --- obj[i] = '';

gnarf
  • 105,192
  • 25
  • 127
  • 161
  • This is basically the same answer as @Vincent suggested, but includes code... I really don't see this as a solution to the question. – Robert Koritnik Sep 10 '09 at 08:49
  • All parameters will be turned into a string in the URL. There is no way around that, so you must a) omit the parameter, b) set it to some specific value that you detect in your controller, i.e. 'null' or even `\01` – gnarf Sep 10 '09 at 08:54
  • The question was how to correctly pass null values through to the server so they will get binded to actual nulls and not "null" strings – Robert Koritnik Sep 10 '09 at 08:54
  • @gnarf: I'm pretty sure this is possible of doing. Maybe it requires creating a custom ModelBinder, but I guess it should be possible. – Robert Koritnik Sep 10 '09 at 08:56
  • This part of your question: If I specify only parameters that are not null (without null parameters) in my javascript call, the parameters in my action are transferred correctly - sounds like if you remove the null values from your data object, it works the way you wanted it to, right? – gnarf Sep 10 '09 at 08:58
  • @gnarf: the non passed parameters would of course bind to null, the bigger picture here is that ModeBinder should bind Ajax as well as POST requests that can't pass null valued parameters unless omitted (ie. ?NullParam=&NonNull=123 which could be binded to either null or string.Empty for NullParam parameter) – Robert Koritnik Sep 10 '09 at 08:59
  • 1
    When jQuery assembles its data it uses a helper function called `$.param` - that function encodes `null` as the string `'null'`. I think you could pass `''` or no parameter easily. Empty form fields default to `''`... – gnarf Sep 10 '09 at 09:02
  • One more change... stripNull should go recursively through elements when they are either arrays of objects to strip nulls out of them as well. – Robert Koritnik Sep 15 '09 at 22:06
  • I've just made a Stack Overflow topic on this: **[URL parameters without values](http://stackoverflow.com/questions/11088184/url-parameters-without-values)** – hippietrail Jun 18 '12 at 19:19
0

can't you just call $.getJSON without username parameter if it's null ?

I mean, do you really need to pass username, firstname ... when you wan't to check the strength of a password ?

vvo
  • 2,653
  • 23
  • 30
  • 1
    It makes the code simpler to include all values in JSON object, because otherwise he should be checking each and single value and add non-null values in. I think he's asking how to correctly pass null values to server and not how to omit them. :) – Robert Koritnik Sep 10 '09 at 08:20