代码之家  ›  专栏  ›  技术社区  ›  MyNameIsGuzse

ASP.NET json对象不包含自定义对象

  •  1
  • MyNameIsGuzse  · 技术社区  · 5 年前

    当用户到达某个位置时,他将得到一个问题。因此,我有一个“问题”类和一个“地点”类。然而,当我检索一个位置时,Question参数总是空的。

    初始化数据库时创建对象:

    public static void Initialize(DBContext context)
        {
            context.Database.EnsureCreated();
            if (!context.Games.Any())
            {
                var teams = new List<Team>();
                var team1 = new Team()
                {
                    TeamName = "Kwizmasterz",
                    TotalPoints = 0,
                    TotalBoobyTraps = 2
                };
                var team2 = new Team()
                {
                    TeamName = "Xesennettet",
                    TotalPoints = 0,
                    TotalBoobyTraps = 2
                };
                teams.Add(team1);
                teams.Add(team2);
    
                var game = new Game()
                {
                    GameCode = "X35H0",
                    team = teams
                };
    
                context.Games.Add(game);
                context.SaveChanges();
            }
    
            if (!context.Locations.Any())
            {
                var que = new Question()
                {
                    QuestionText = "How much is 2 + 2?",
                    Answer = "4",
                    IsSolved = false,
                    Points = 1000000
                };
                var loc = new Location()
                {
                    LocationName = "LocationName",
                    Latitude = 50.2299036,
                    Longitude = 5.4163052,
                    Question = que,
                    IsBoobyTrapped = false
                };
    
                context.Locations.Add(loc);
                context.SaveChanges();
            }
        }
    

    public class Location
    {
        public int LocationID { get; set; }
        public string LocationName { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public Question Question { get; set; }
        public bool IsBoobyTrapped { get; set; }
        public int VictorTeamID { get; set; } = -1;
    }
    

    public class Question
    {
        public int QuestionID { get; set; }
        public int QuestionType { get; set; } // 1 = Question - Answer
    
        public string QuestionText { get; set; }
        public int Points { get; set; }
        public bool IsSolved { get; set; }
    
        public string Answer { get; set; }
    }
    

    控制器类:

    [Route("api/v1")]
    public class GameController : Controller
    {
        private readonly DBContext context;
        public GameController(DBContext context)
        {
            this.context = context;
        }
    
        public IActionResult Index()
        {
            return View();
        }
    
        [Route("location")]
        [HttpPost]
        public IActionResult postGame([FromBody] Location newLocation)
        {
            newLocation.LocationID = context.Games.Count();
            context.Locations.Add(newLocation);
    
            return Created("", newLocation);
        }
    
        [Route("location")]
        [HttpGet]
        public List<Location> getLocations()
        {
            return context.Locations.ToList();
        }
    
        [Route("location/{id}")]
        [HttpGet]
        public Location getLocation(int id)
        {
            int _id = id - 1;
            List<Location> loc = context.Locations.ToList();
            if (loc[_id] != null)
                return loc[_id];
            else
                return null;
        }
    
        [Route("game")]
        [HttpPost]
        public IActionResult postGame([FromBody] Game newGame)
        {
            newGame.GameID = context.Games.Count();
            context.Games.Add(newGame);
    
            return Created("", newGame);
        }
    
        [Route("game")]
        [HttpGet]
        public List<Game> getGames()
        {
            return context.Games.ToList();
        }
    
        [Route("game/{id}")]
        [HttpGet]
        public Game getGame(int id)
        {
            List<Game> game = context.Games.ToList();
            if (game[id] != null)
                return game[id];
            else
                return null;
        }
    }
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   H.Mikhaeljan    5 年前

    Link

    你可以使用 Include("Question") 所以完整的语法是:

    context.Locations.Include("Question") 因此,您将在检索位置时包含该问题

    您还可以通过链接它们来执行多个包含 context.Locations.Include("Question").Include("SomethingElse")

    编辑 就像我在你的代码里看到的 getLocation 仍然不使用include。请参阅下面的正确使用方法

    public Location getLocation(int id)
    {
        int _id = id - 1;
        List<Location> loc = context.Locations.Include("Question").ToList();
        if (loc[_id] != null)
            return loc[_id];
        else
            return null;
    }
    

    第二次编辑 因为你先把整个名单拉出来,然后得到一个位置

    public Location getLocation(int id)
    {
        int _id = id - 1;
        //FirstOrDefault will return automatically a null if the id cannot be found. 
        //This will result in only getting the single Location from context instead the complete list
        return context.Locations.Include("Question").FirstOrDefault(x=>x.id == _id);
    }