title: Pass data from View to Controller using Ajax
body: <p>I know this question has been asked here thousands of times ; i'm already following many things i've read but i still can't get this to work.</p>
  
  <p>What i'm basically trying to do is send an FCM Token (that i obtained with a script on my basis Layout.cshtml) to the controller, by using Ajax. However, the value is always null. Even following the correct names i can't get the token data on the controller. So far so good this is what i've done.</p>
  
  <p>HTML Page:</p>
  
  <pre><code>    @using RazorEngine.Templating
  @inherits TemplateBase
  
  @{
      Layout = "..\\Layout.cshtml";
  }
  &lt;script src="https://code.jquery.com/jquery-3.4.1.min.js"
          integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
          crossorigin="anonymous"&gt;&lt;/script&gt;
  &lt;script&gt;
      function submitForm() {
          var currToken = messaging.getToken();
          var TokenModel = new Object();
          TokenModel.RegisteredToken = currToken;
          $.ajax(
              {
                  type: "POST", //HTTP POST Method
                  url: "Notification/Register", // Controller/View 
                  dataType: 'json',                
                  data: JSON.stringify({
                      Token: TokenModel                    
                  },
                  success: function (status) {
                      console.log('Send');
                  },
                  error: function () {
                      console.log('something went wrong - debug it!');
                  }
              });
      }
  &lt;/script&gt;
  &lt;div id="token"&gt;&lt;/div&gt; 
  &lt;input type="button" value="Click" onclick="submitForm()" /&gt;
  </code></pre>
  
  <p>Controller (i'm sending everything to the Register Action)</p>
  
  <pre><code>public class NotificationController : BaseController
      {
          [HttpPost]
          [AllowAnonymous]
          public async Task&lt;IHttpActionResult&gt; Register(TokenModel Token)
          {
  
              //TokenModel currtoken = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;TokenModel&gt;(myToken);
  
  
              string token = "agamenon";
              string secondToken = "nosferatu";
              var execCtx = this.ExecutionContext;
  
              var pushNotifService = execCtx.GetExtension&lt;IPushNotificationService&gt;(); 
  
  
  
              var notificationTitle = "tituloX" + new Random().Next(1000, 5000);
              var notificationBody = "someContent" + new Random().Next(91000, 95000);
              var msg = new PushNotificationMessage()
              {
                  Body = notificationBody,
                  Title = notificationTitle
              };
              await pushNotifService.SendMessageAsync(msg, new string[] { token,secondToken });
              //var info = await pushNotifService.GetInfoAsync(token);
              //var invalidInfo = await pushNotifService.GetInfoAsync(secondToken);
              return View();
          }
  </code></pre>
  
  <p>And the TokenModel class:</p>
  
  <pre><code>public class TokenModel
  {
      public string RegisteredToken { get; set; }
  }
  </code></pre>
  
  <p>This site doesn't follow the MVC model, its an WebAPI. What am i doing wrong that i can't get the data sent to the controller ? AFAIK names are okay, everything is okay, its supposed to work.</p>
  
  <p>Any input is appreciated.</p>
  
  <p>Thanks</p>