Reputation: 379
I found this question that suggested I should be able to accomplish my goal. I want to return only posts where the parent_post field is null.
Here's how my data looks in my table that it's pulling from:
create table dbo.post
(post_id int not null primary key,
thread_id int not null,
parent_post int,
creator int not null,
post_text nvarchar(max) not null)
insert into dbo.post (post_id, thread_id, parent_post, creator, post_text) values
(1,1,null,123,'Here is the first post in the forum!'),
(2,1,null,123,'Here is the second post!'),
(3,1,2,123,'Here is a reply! Let''s hope it works!'),
(4,1,2,123,'Here is a second reply. I wonder how this looks?'),
(5,1,3,123,'Here is a third level deep reply. ')
In my controller for my API, it looks like this:
[HttpGet]
[Route("api/threads/{thread_id}")]
[AcceptVerbs("GET")]
public IEnumerable<post> GetPostsByThread(long thread_id)
{
return db.post.Where(post => post.thread_id == thread_id && post.parent_post == null).ToList();
}
(I got rid of the piece from the linked question that used "Trim" because my blanks are never going to be white space)
Now, I would expect that this would return the records for posts 1 and 2. However, when I call the API, it returns nothing. When I go to http://localhost:#####/api/threads/1 (or any other number), it returns this:
<ArrayOfpost xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Test_Forum.Models"/>
Edited to add the Model for Post
namespace Test_Forum.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class post
{
[Key]
[Required]
[Column(Order = 0)]
public long post_id { get; set; }
[Required]
[Column(Order = 1)]
public long thread_id { get; set; }
[Required]
[Column(Order = 2)]
public long? parent_post { get; set; }
[Required]
[Column(Order = 3)]
public long creator { get; set; }
[Required]
[Column(Order = 4)]
public DateTime create_date { get; set; }
[Required]
[Column(Order = 5)]
public DateTime update_date { get; set; }
[Required]
[Column(Order = 6)]
public string post_text { get; set; }
}
}
Upvotes: 0
Views: 101
Reputation: 500
There is a simple mismatch between the post model class and the database table. I get back the data using the following model for post:
public post {
[Key]
[Required]
[Column(Order = 0)]
public long post_id { get; set; }
[Required]
[Column(Order = 1)]
public long thread_id { get; set; }
//[Required]
[Column(Order = 2)]
public long? parent_post { get; set; }
[Required]
[Column(Order = 3)]
public long creator { get; set; }
//[Required]
[Column(Order = 4)]
public DateTime? create_date { get; set; }
//[Required]
[Column(Order = 5)]
public DateTime? update_date { get; set; }
//[Required]
[Column(Order = 6)]
public string post_text { get; set; }
}
Change the class and update the database and it will work fine
Upvotes: 2