title: ASP.NET Web API 2 - Nested Object in Ajax Post coming across as null
body: <p>I'm attempting to write a MVC/Web API page, and am stuck on the Web API post portion when doing more than just base types. I cannot seem to find an answer why the Roles object keeps getting set to null even though it's posting from the client side (and it's passing the ModelState.IsValid portion?) </p>
  
  <p>If I take all the code out that relates to the Roles portion, it works and the user gets created in the database. </p>
  
  <p>Edit: Changing [FromUri] to [FromBody] combined with adding Bracket around Roles data seemed to do it. Other errors popping up but able to take it from there. Thanks. </p>
  
  <p>The defined model:</p>
  
  <pre><code>public class ExpandedUser
  {
  
          [Key]
          [Display(Name = "User Name")]
          public string UserName { get; set; }
          public string Email { get; set; }
          public string Password { get; set; }
          [Display(Name = "Lockout End Date UTC")]
          public DateTime? LockoutEndDateUtc { get; set; }
          public int AccessFailedCount { get; set; }
          public IEnumerable&lt;UserRoles&gt; Roles { get; set; }
  }
  </code></pre>
  
  <p>The API: </p>
  
  <pre><code>    [HttpPost]
      public IHttpActionResult CreateUser([FromUri] ExpandedUser user)
  
      {
  
          if (!ModelState.IsValid)
          {
              return BadRequest();
          } else if (user.Roles == null)
          {
              return NotFound();
          }
  
          var userStore = new UserStore&lt;ApplicationUser&gt;(_context);
          var userManager = new UserManager&lt;ApplicationUser&gt;(userStore);
  
  
          /*var roleId = user.Roles.ToList();
  
         var role = _context.Roles.Select(c =&gt; new
         {
             Id = c.Id,
             Name = c.Name,
             Users = c.Users
         }).SingleOrDefault(c =&gt; c.Id == roleId.RoleName);*/
  
  
  
          var newUser = new ApplicationUser
          {
              UserName = user.UserName,
              Email =  user.Email,
              PasswordHash = userManager.PasswordHasher.HashPassword(user.Password)
  
      };
  
  
          try
          {
              _context.Users.Add(newUser);
              /*userManager.AddToRole(newUser.Id, role.Name);*/
              _context.SaveChanges();
  
  
          }
          catch
          {
              return BadRequest();
          }
  
          return Ok();
      }
  </code></pre>
  
  <p>And the AJAX call: </p>
  
  <pre><code>  var payload = {
              Id: 0, UserName: $('#username').val(),
              Email: $('#email').val(),
              Password: $('#password').val(),
              LockoutEndDateUtc: null,
              AccessFailedCount :0,
              Roles: {
              RoleName: $('#role').val()
          }
          };
          payload = JSON.stringify(payload);
          console.log(payload);
          $.ajax({
              type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "/api/CreateUser",
              data: payload,
              success: function (data) {
                  redir();
              },
              error: function (jqXHR, textStatus, errorThrown) {
  
              },
              dataType: "json",
          });
  </code></pre>