0

Javascript code:

var x = null;
var action_data = {x:x};
$.get(
  '~/MyController/MyAction',
  action_data,
  function(result){
    //do_something
  }
);

Controller action:

public class MyController: Controller{
  ...
  public ActionResult MyAction(string x)
  {
    //here, x is the string 'null';

  }
}

Can someone explain to me why the string "null" is sent to the action instead of the value null? Thanks

1
  • because u set var x = null; it is passed as string Commented Dec 12, 2013 at 9:07

2 Answers 2

1

because u passed data null which converted to string and set to your action:

var x = null;
x="any thing"; // you need to change x value to be sent here
var action_data = {x:x};
$.get(
  '~/MyController/MyAction',
  action_data,
  function(result){
    //do_something
  }
);

if you want to pass null:

var x;
var action_data = {x:x};
$.get(
   '~/MyController/MyAction',
   action_data,
   function(result){
     //do_something
   }
 );
Sign up to request clarification or add additional context in comments.

1 Comment

I actually want the value to be null in some cases. So in my method, i want (x == null) to be true
0

Ok, so thanks to Ahmed, i came up with the solution:

var x; //without the null initialization

does the trick ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.