Thursday, April 4, 2019

Cannot update context in razor pages

I'm writing a program to do some data analysis. An analysis consists of multiple results for multiple standards. The database is set up as a M:M between Analysis and Standards, with StandardResults as the join table.

Relevant parts of PageModel:

public class TestModel : PageModel { private readonly Project.Models.ProjectContext _context; public TestModel(Project.Models.ProjectContext context) { _context = context; } [BindProperty] public IList<Analysis> Analyses { get; set; } [BindProperty] public List<Models.Standard> Standards { get; set; } public async Task<IActionResult> OnGetAsync(int? id) { if (id == null) { return NotFound(); } Analyses = await _context.Analysis .Where(a => a.RID == RID) .Include(a => a.StandardResults) .ThenInclude(s => s.Standard) .ToListAsync(); Standards = await _context.Standard .Include(s => s.StandardResults) .ToListAsync(); return Page(); } public async Task<IActionResult> OnPostExpectedValueAsync(int? id) { await _context.SaveChangesAsync(); return RedirectToPage("/R/Test", new { id = id }); } } 

When a user creates a new analysis, the view will then ask for the user to enter the expected values. The form is shown partially below - just omitting the submit button and form tags. The submit button routes to the OnPostExpectedValueAsync handler.

<!-- Gets the standards that are associated with this analysis --> @{ var activeStds = a.StandardResults.Select(sr => sr.Standard).Distinct().ToList(); } @for (int i = 0; i < activeStds.Count(); i++) { <!-- Find the index in the context of the active standard --> int index = Model.Standards.FindIndex(s => s.ID == activeStds[i].ID); <input hidden asp-for="Standards[index].ID" /> <input hidden asp-for="Standards[index].Etching" /> <div class="form-group row"> <div class="col-2 font-weight-bold">@Model.Standards[index].Etching</div> <input class="col-2 form-control" asp-for="Standards[index].ExpectedO" /> <input class="col-2 form-control" asp-for="Standards[index].ExpectedP" /> <input class="col-2 form-control" asp-for="Standards[index].ExpectedC" /> </div> } 

When I use Standards[index], the debugger sees no items in the list Standards. However, if I change one of the Standards[index] to Standards[i] then it does recognize it, but it still doesn't change any values.

If you can't tell, I'm super confused. Please help!

Cannot update context in razor pages Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team