I've got a Web API controller in .NET Core. I'm posting data to save a new comment
model, and I have the AuthorID
property that has a [Required]
data annotation on the model.
I'm setting the AuthorID
in the controller before saving. However, ModelState is always coming back stating that AuthorID field is required.
How do I keep my validations on the model in place and validate correctly in this scenario?
public async Task<IActionResult> PostComment([FromBody] Comment comment)
{
comment.AuthorID = Utilities.GetUserId(this.User);
comment.CreatedAt = DateTime.Now;
if (!ModelState.IsValid) {
....