title: JSON to MVC Api Controller via AJAX
body: <p>So I'm trying to add a "like" function to some blog posts by POSTing some JSON through ajax to the MVC api controller.</p>
  
  <p>Models:</p>
  
  <pre><code>public class Blog{
    public int ID{get;set;}
    public string Title{get;set;}
    //Some more stuff
    public virtual ICollection&lt;Likes&gt; Likes {get;set;}
  }    
  
  public class Likes{
    public int ID{get;set;}
    public string UserName{get;set;}
    //Some more stuff
    public virtual Blog Blog{get;set;}
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public void Post(Likes like){
    if(ModelState.IsValid){
      db.Likes.Add(like)
      db.SaveChanges();
    }
  }
  </code></pre>
  
  <p>The below <code>return</code> is passed into the <code>JSON.stringify()</code> in the ajax call:</p>
  
  <pre><code>var UserName = "Dave";
  //More stuff
  var Blog = "????";
  
  return {UserName:UserName, More Stuff, Blog:Blog}
  </code></pre>
  
  <p>So that last line is my problem I think, what do I pass in for "Blog"? I have tried using the ID of the Blog but it's still null in the controller. It seems the controller wants a full Blog model instead of just the ID, is there a way around this or am I doing something bad?</p>
  
  <p>TLDR I guess is: What to pass to a field that's a <code>public virtual Blog Blog{get;set;}</code> in the model when sending JSON to the controller?  </p>
  
  <p>P.S. My code works when I don't include the "Blog" as part of the JSON.</p>