I am trying to add new data and on "Save" to automatically refresh data. However, on save I get error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
RoSafety.Pages.PTW.IsoC.IsoCertModel.ICData.get returned null.
If I use a separate page for Post, the data is saved but on same page as OnGet is not. How should I do it?
- [BindProperty]
- public ICContent ICContent { get; set; }
- public ICData ICData { get; set; }
-
- public async Task<IActionResult> OnGetAsync()
- {
- ICData = await _context.ICDatas
- .Include(a=>a.ICContents)
- .AsNoTracking()
- .FirstOrDefaultAsync(a => a.IdICD == 1);
-
- if (ICData == null)
- {
- return NotFound();
- }
- return Page();
- }
-
- public async Task<IActionResult> OnPostAsync()
- {
- if (!ModelState.IsValid)
- {
- return Page();
- }
-
- var emptyIsoCert = new ICContent();
-
- if (await TryUpdateModelAsync(emptyIsoCert, "ICContent",
-
- s => s.Equipment,
- s => s.EnergSource,
- s => s.IsoType,
- s => s.IsoMethod,
- s => s.LockNo,
- s => s.PreparedBy,
- s => s.ImplementedBy,
- s => s.VerifiedBy,
- s => s.IsoStatus ))
-
- {
- _context.ICContents.Add(emptyIsoCert);
- await _context.SaveChangesAsync();
-
- return await OnGetAsync();
- }
-
- return null;
- }