I'm building a simple app for entertainment with ASP.NET Core where I currently just have a Post
model and a User
model.
Post.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public class Post
{
[Required]
public int Id { get; set; }
[Required]
[MaxLength(16)]
public string Title { get; set; }
public string Body { get; set; }
[Required]
public Person Author { get; set; }
}
}
Person.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public class Person
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public ICollection<Post> Posts = new List<Post>();
}
}
I used Visual Studio's scaffolding to generate controllers for those models.
When I'm trying to POST
/api/posts
in order to create a new post, I want also to specify the Author ID
for that post.
I tried adding the attribute authorId
, I tried "authorId": 1
and also "author": { "id": 1 }
, but because the Author is required, I always get back either Author is missing
or Email in author is missing
etc.
How can I include the authorId
for in the post body in order I would be able to successfully create a new post?