0

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?

shabenda
  • 1,759
  • 3
  • 14
  • 23
  • Could you please summarize your problem in the question title, instead of stuffing the question title with tags/keywords? Take a moment and read [What are tags, and how should I use them?](https://stackoverflow.com/help/tagging). A good title which summarizes the issue you are having, helps others to spot your question and see from the title alone if they have a solution for it or not – Tseng Nov 13 '17 at 18:50
  • Your editing data, so always use a view model, and that view model will not contain a property for `Author` because it will not be edited by the user.In the POST method you initialize your data model (or get it from the db if editing an existing record) and set its properties based on the view model (and set the Author property based on the current user) [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Nov 13 '17 at 19:51

2 Answers2

0

I am not sure what is your current controller method, as you havent posted that here to see what is wrong, but to give you a general idea, this is how it should look like

Note : Am unsure why you would want to have the author Id as a query string for HTTPPOST, as the authorId would be part of author property of the post model itself and that you can always verify if(IsValid(postModel.AuthorId)) and if not valid, based on your business logic, create the author first and then the post or else send a response saying badrequest and that the author must exist

   [HttpPost]
   [Route("{authorid}")]
   public Task<IActionResult> MyPostMethod(string authorid, [FromBody] Post postModel)
    { 
            // yourWork
    }
Jaya
  • 3,721
  • 4
  • 32
  • 48
0

Your controller will be expecting a valid whole Person class for the Author property (which means you would have to provide all the properties that are required for a Person) - try changing your Post class to include just the AuthorId as a required field. You can still include Author as a property, just dont make it required.

public class Post
{
    [Required]
    public int Id { get; set; }

    [Required]
    [MaxLength(16)]
    public string Title { get; set; }

    public string Body { get; set; }

    [Required]
    public int AuthorId { get; set; }

    public Person Author { get; set; }
}
Steve Land
  • 4,852
  • 2
  • 17
  • 36