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?

2
  • When you set parameters to undefined value, do they also get ModelBinded as "undefined" strings? Commented Sep 10, 2009 at 8:18
  • yes, they have also "undefined" values as a string Commented Sep 10, 2009 at 8:29

2 Answers 2

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] = '';

Sign up to request clarification or add additional context in comments.

9 Comments

This is basically the same answer as @Vincent suggested, but includes code... I really don't see this as a solution to the question.
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
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
@gnarf: I'm pretty sure this is possible of doing. Maybe it requires creating a custom ModelBinder, but I guess it should be possible.
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 ''...
|
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 ?

1 Comment

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. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.