0

I am working on an API method that should accept an Image model object, which has a property List<Comment> Comments. The Image POSTed from the mobile app works fine but if I include an array of Comment objects they aren't showing up on the instance of the Image. I'm not super great with C# so any help would be appreciated.

Image Class

public class Image
{
    public int? ImageId { get; set; }
    [Required]
    public string Image { get; set; }
    [Required]
    public string ContentType { get; set; }
    [Required]
    public string Filename { get; set; }
    [Required]
    public DateTime DateTaken { get; set; }
    [Required]
    public int UserId { get; set; }
    [Required]
    public int CompanyId { get; set; }
    [Required]
    public int LocationId { get; set; }
    public decimal? Lat { get; set; }
    public decimal? Long { get; set; }
    public List<ApiComment> Comments { get; set; }
}

Comment Class

public class ApiComment
{
    [Required]
    public string Comment { get; set; }
    [Required]
    public DateTime DateCreated { get; set; }
    [Required]
    public int UserId { get; set; }
}

Beginning of ImagesController

public class ImagesController : ApiController
 {
    [System.Web.Http.HttpPost]
    public ActionResult Post(Image image)
CWitty
  • 4,488
  • 3
  • 23
  • 40

1 Answers1

0

I believe there's something wrong with the request body of your request:

I tried this in fiddler:

{ "Comments" : [{ "Comment" : "Hello"}, {"Comment" : "World"}]}

And I got two counts for Comments in my WebAPI Action Method.

Check there are no typo error with the request object you are posting and the Json is valid.

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
  • The body looks like `Comments[][Comment]=Test&Comments[][DateCreated]=2015-07-13T14%3A56%3A53&Comments[][UserId]=105` So I am guessing the missing index in the `[]` brackets is what is wrong. – CWitty Jul 13 '15 at 14:59
  • Sorry the error seemed to be iOS side, the body was encoded using AFNetworking's AFHttpRequestSerialier and should have been AFJsonRequestSerializer. You post helped me figure that out though, thanks – CWitty Jul 13 '15 at 15:06