I have one class Game
public class Game
{
public int GameId { get; set; }
public User XUser { get; set; } = new User();
and other one User
public class User
{
public int UserId { get; set; }
public string UserName { get; set; } = "Computer";
}
On the page I have information about objects and can add new one
@foreach (var item in Model.Game) {
<tr>
<td>@item.XUser.UserName</td>
</tr>
} When i trying to add new object Game, I want that user name was rewrited
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
Game.XUser.UserName = "xxx"; // this is my string, but nothing has changed
_context.Games.Add(Game);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
But I see always "Computer". I am new in FE, dont know what I m doing wrong?
#ANSWER: There is need UserId in the class Game
public int UserId { get; set; }