title: Ajax passing empty value but Controller get null in ASP.NET MVC
body: <p>I'm working with <code>ASP.NET MVC</code> and have a problem with the value sent from <code>Ajax</code> to my controller.</p>
  
  <p>Let's say I have <strong><code>SampleViewModel</code></strong> like this:</p>
  
  <pre><code>public class SampleViewModel
  {
      private string _firstName = string.Empty;
  
      public SampleViewModel()
      {
          _firstName = string.Empty;
      }
  
      public string FirstName
      {
          get { return _firstName; }
          set { _firstName = value ?? string.Empty; }
      }
  
      public string LastName { get; set; }
  
      public string FullName { get; set; }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>[HttpPost]
  public JsonResult ActionSubmit(SampleViewModel model)
  {               
      var result = "";
  
      if(model.FirstName == null)
            result += "\nFirstName is null";
  
      if(model.LastName == null)
            result += "\nLastName is null";
  
      return Json(result);
  }
  </code></pre>
  
  <p><strong>Ajax</strong></p>
  
  <pre><code>$('.submit').click(function() {
          $.ajax({
              url: '@Url.RouteUrl(new{ action="ActionSubmit", controller="Home"})',
              data: JSON.stringify({ FirstName: '', LastName: '', FullName: 'Phong_Nguyen' }),
                    // Even though I use { FirstName: '', LastName: '', FullName: 'Phong_Nguyen' } without JSON.stringify
              type: 'POST',
              dataType: 'json',
              contentType: "application/json; charset=utf-8",
              success: function(resp) {
                     alert(resp);
              }});
           });
  </code></pre>
  
  <p>As you can see, I send empty value but on the controller's end, I get null (the response value always "LastName is null"):
  <a href="https://i.stack.imgur.com/reBae.png" rel="noreferrer"><img src="https://i.stack.imgur.com/reBae.png" alt="enter image description here"></a></p>
  
  <p><strong>Question</strong></p>
  
  <ol>
  <li><p>Why is it that when in Ajax I am sending <code>empty</code>, I get <code>null</code> value in my controller?</p></li>
  <li><p>Is there a better way and more elegant to resolve my problem like below?</p></li>
  </ol>
  
  <pre><code>public string FirstName
  {
     get { return _firstName; }
     set { _firstName = value ?? string.Empty; }
  }
  </code></pre>