Home > front end >  GET Request with ID Param and PUT Request do not affect my Model class within my Model Class
GET Request with ID Param and PUT Request do not affect my Model class within my Model Class

Time:09-21

My GET Request with ID as a parameter does not properly load all my data. My PUT Request successfully alters the data according to Swagger but then the GET Requests show that no change has been made. My GET Request with a parameter says my Player data is null when it shouldn't be. My normal GET Request loads the data properly, except when data within the PUT Request changes something within Player[]. See my code below.

Player Model:

public class Player
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Teams Model:

public class Team
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public IEnumerable<Player> Players { get; set; }
}

Generic GET Request (returns POST'ed data but not updated Player data from PUT Request):

[HttpGet]
public async Task<ActionResult<IEnumerable<Team>>> GetTeams()
{
    return await _context.Teams.Include(t => t.Players).ToListAsync();
}

GET Request with ID Parameter (doesn't load Player data):

[HttpGet("{id}")]
public async Task<ActionResult<Team>> GetTeam(long id)
{
    var team = await _context.Teams.FindAsync(id);

    if (team == null)
    {
        return NotFound();
    }

    return team;
}

PUT Request (Alters data):

[HttpPut("{id}")]
public async Task<IActionResult> PutTeam(long id, Team team)
{
    if (id != team.Id)
    {
        return BadRequest();
    }

    _context.Entry(team).State = EntityState.Modified;

    try
    {
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!TeamExists(id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }

    return NoContent();
}

GET Request with ID Parameter response:

{
  "id": 1,
  "name": "panthers",
  "location": "string",
  "players": null
}

Essentially, my PUT Requests returns 204 and Swagger says I've altered the data. The changes specifically within the Player list are not altered, whereas anything aside from it like Location will change just fine. Additionally, the GET Request with ID doesn't return Player at all. GET shows Player data, but not changes within my PUT Request.

CodePudding user response:

To GET Players for a specific team by id, the principle is the same, eager load the navigation property:

var team = await _context.Teams.Include(t => t.Players).FindAsync(id);

For the PUT request, I'd say you should have an Update or UpdateAsync method:

 _context.Teams.Update(team);
 await _context.SaveChangesAsync();

If this, doesn't work or is unavailable you can try and mark the players as modified as well:

_context.Entry(team).State = EntityState.Modified;

foreach(var player in team.Players)
{
    _context.Entry(player).State = EntityState.Modified;
}
await _context.SaveChangesAsync();

If it's still doesn't work you can load the entity, that will track the entity, and detect changes:

var teamFromBd = await _context.Teams.Include(t => t.Players).FindAsync(id);    
teamFromBd = team; //*
await _context.SaveChangesAsync();

Make sure the Players are indeed binded to the team method parameter.

* The above is a simplification, you should need to assign the team properties one by one (except fot Ids). You can try as is but it probably won't work.

  • Related