Deliver JSON nested in WebAPI .net core

1

I need to deliver a JSON that has a TimelineItem where each timelineitem corresponds to a user.

User class

    public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Class timelineitem

    public class TimelineItem
{
    public int Id { get; set; }
    public string Content { get; set; }
    public string Title { get; set; }
    public User User { get; set; }
}

My get in the api

    // GET: api/TimelineItems
    [HttpGet]
    public IEnumerable<TimelineItem> GetTimelineItems()
    {
        return _context.TimelineItems;
    }

JSON who gives me

[{"id":2,"content":"fsdaj fdsfa fsdjklafjlk fdjsaklfjsad fjdsklf  fdsjakl","title":"fjdskjfsdakl","user":null},{"id":3,"content":"fsdjk wqeui ietuw q tweti gj vcm v,cmv vmc,v c","title":"ureiwu","user":null},{"id":4,"content":"hola amigos del yu tub","title":"fdsfsda","user":null}]

All users return null.

I suppose I should change the TimelineItem model?

    
asked by PCH 19.12.2017 в 19:12
source

2 answers

2

As in effect you must modify your TimelineItem model since you have defined a navigation property but you have not declared UserId as an attribute. Try this:

public class TimelineItem
{
public int Id { get; set; }
public string Content { get; set; }
public string Title { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}

Responding to your other doubt. The user leaves you as null because EF does not load the corresponding data to him since you are not asking for User but for TimelineItem for what EF is limited to offer you only information referring to TimelineItem , this is thought In order to improve the performance in queries to BD, this is known as Lazy Loading or Lazy Loading. If you want your Json to show the User of each TimelineItem you must do Include in your query LINQ example:

    // GET: api/TimelineItems
    [HttpGet]
    public IEnumerable<TimelineItem> GetTimelineItems()
    {
        return _context.TimelineItems.Include(t=>t.User);
    } 

In this way your Json returns an Object User for each TimelineItem and you can access any property of it from the @model.user.xxx . I hope it helps you

    
answered by 19.12.2017 / 19:58
source
0

I solved it in the following way, after following the recommendation of Vicente Almea

And add

public class TimelineItem
{
   public int Id { get; set; }
   public string Content { get; set; }
   public string Title { get; set; }
   public int UserId { get; set; }
   public User User { get; set; }
}

I went to my controller and modified the get method in the following way:

        // GET: api/TimelineItems
    [HttpGet]
    public IEnumerable<TimelineItem> GetTimelineItems()
    {
        foreach (TimelineItem item in _context.TimelineItems)
        {
            item.User = _context.Users.Find(item.UserID);
        }

        return _context.TimelineItems;
    }

I guess there would be some way to do foreach like LINQ but it worked for me.

    
answered by 19.12.2017 в 21:14