作者:廊坊0316慢摇酒吧_196 | 来源:互联网 | 2023-08-11 14:05
ForsomereasononlythefirstitemofeacharrayisbeingreturnedasJSON,anyclueswhy?由于某种原因,每
For some reason only the first item of each array is being returned as JSON, any clues why?
由于某种原因,每个数组的第一个条目被返回为JSON,有什么线索吗?
Here is what I see during debugging, as you can tell, I have two items in 'Category' and two items in 'Tasks':
下面是我在调试过程中看到的,正如您所看到的,我在“Category”中有两个条目,在“Tasks”中有两个条目:
Postman JSON result (it should return all items, shouldn't it?):
Postman JSON结果(它应该返回所有项,不是吗?)
For reference, here is my 'Category.cs':
以下是我的“类别。c”:
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public DateTime Timestamp { get; set; }
public string Username { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public virtual ICollection Tasks { get; set; }
}
My 'Task.cs':
我的“Task.cs”:
public class Task
{
public int TaskId { get; set; }
public string Name { get; set; }
public DateTime Timestamp { get; set; }
public virtual Category Category { get; set; }
}
and my Api:
和我的Api:
[HttpGet]
public JsonResult Get()
{
var result = _repo.GetAllForUser("lucas@test.com");
return Json(result);
}
And repository:
和存储库:
public IEnumerable GetAllForUser(string name)
{
return _ctx.Categories
.Where(c => c.ApplicationUser.UserName == name)
.Include(c => c.Tasks)
.ToList();
}
Here is what I insert into database, and what I should retrieve from the Api:
以下是我插入到数据库中的内容,以及我应该从Api中获取的内容:
categories.Add(new Category
{
Name = "cat 1",
Tasks = new List
{
new Task { Name="task 1" },
new Task { Name="task 2" }
}
});
categories.Add(new Category
{
Name = "cat 2",
Tasks = new List
{
new Task { Name="task 3" },
new Task { Name="task 4" }
}
});
2 个解决方案