title: ASP.NET MVC - How to pass a Javascript variable from view to controller
body: <p>I have a variable in a javascript function which needs to be sent to the controller. This is the code in the javascript function.</p>
  
  <pre><code>var testString = "Test";
  
          $.ajax({
          type: "POST",
          url: "@Url.Action("GetJavaScriptString")",
              dataType: "json",
              data: JSON.stringify(testString),
          success: function (data) {
              alert(data);
          },
          failure: function (errMsg) {
              alert(errMsg);
          }
          });
  </code></pre>
  
  <p>And this is the controller method</p>
  
  <pre><code>    public ActionResult GetJavaScriptString(string data)
      {
  
          return null;
      }
  </code></pre>
  
  <p>The variable "data" in the GetJavaScriptString method remains null.</p>