Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 145.6k

ASP.NET Core Razor Page OnPost with parameter

Oct 26 2020 10:53 AM
I have a Razor page which OnGet receive a parameter from another page and load the data. The OnGet looks like below
  1. public async Task<IActionResult> OnGetPTWNoIdAsync(int ptwNoId)    
  2. {    
  3.     some code here    
  4. }   
OnPost looks like below
  1. public async Task<IActionResult> OnPostHazIdAsync()    
  2. {    
  3.     if (!ModelState.IsValid)    
  4.     {    
  5.         return Page();    
  6.     }    
  7.     
  8.     var newHazId = new HazId();    
  9.     if (await TryUpdateModelAsync(newHazId, "HazId",    
  10.         c => c.PTWNo,    
  11.         c => c.HazClass,    
  12.         c => c.HazDetails,    
  13.         c => c.HazPMClass,    
  14.         c => c.HazPMDetails    
  15.         ))    
  16.        {    
  17.            _context.HazIds.Add(newHazId);    
  18.            await _context.SaveChangesAsync();    
  19.            return RedirectToPage(new { ptwNoId = newHazId.PTWNo });    
  20.        }    
  21.     
  22.        if (newHazId == null)    
  23.        {    
  24.            return NotFound();    
  25.         }  
  26.         return RedirectToPage( new { ptwNoId = newHazId.PTWNo });          
  27. }    
However, after OnPost there is no data loaded. Can you help?

Answers (7)