Marius Vasile

Marius Vasile

  • 594
  • 1.9k
  • 144.8k

asp.net core duplicate entry catch error

Jan 19 2021 2:28 PM
I want to prevent duplicate entry on post, which works well when there is no parameter for OnGet. Code below
 
  1. public void OnGet()  
  2.         {  
  3.   
  4.         }  
  5.           
  6.   
  7.         public async Task<IActionResult> OnPostAsync()  
  8.         {  
  9.             if (!ModelState.IsValid)  
  10.             {  
  11.                 return Page();  
  12.             }  
  13.   
  14.             var newWOAL = new WOAssetLocation();  
  15.             if (await TryUpdateModelAsync(newWOAL, "WOAssetLocation", s => s.AssetLocation))  
  16.             {  
  17.                 var check = await _context.WOAssetLocations.Where(s => s.AssetLocation == newWOAL.AssetLocation).CountAsync();  
  18.                 if (check > 0)  
  19.                 {  
  20.                     ModelState.AddModelError(string.Empty, "Asset Location already exists");  
  21.                     return Page();  
  22.                 }  
  23.                 if (check == 0)  
  24.                 {  
  25.                     _context.WOAssetLocations.Add(newWOAL);  
  26.                     await _context.SaveChangesAsync();  
  27.                     return RedirectToPage("/WO/Location/Index");  
  28.                 }  
  29.             }  
  30.   
  31.             return null;  
  32.         }  
 The duplicate is captured and I have error mesage displayed on page. However, when I have OnGet with parameter is not working
 
  1. public async Task<IActionResult> OnGetAsync(string id)  
  2.         {  
  3.             if (id == null)  
  4.             {  
  5.                 return NotFound();  
  6.             }  
  7.             var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);  
  8.             var idorg = await _context.UsersData.Where(u => u.Id == userId).Select(u => u.OrgID).SingleAsync();  
  9.             OrgID = idorg;  
  10.             WOAssetLocation = await _context.WOAssetLocations.Where(m => m.WOALId == id).FirstOrDefaultAsync();  
  11.   
  12.             if (WOAssetLocation == null)  
  13.             {  
  14.                 return NotFound();  
  15.             }  
  16.             return Page();  
  17.         }  
  18.         public async Task<IActionResult> OnPostAsync()  
  19.         {  
  20.             if (!ModelState.IsValid)  
  21.             {  
  22.                 return Page();  
  23.             }  
  24.             var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);  
  25.             var idorg = await _context.UsersData.Where(u => u.Id == userId).Select(u => u.OrgID).SingleAsync();  
  26.             var newWOAA = new WOAssetAsset();  
  27.             if (await TryUpdateModelAsync(newWOAA, "WOAssetAsset",   
  28.                 s => s.WOALId,   
  29.                 s => s.OrgID,  
  30.                 s => s.AssetID,   
  31.                 s => s.AssetName,   
  32.                 s => s.AssetManufacturer))  
  33.             {  
  34.                 var check = await _context.WOAssetAssets.Where(s => s.AssetID == newWOAA.AssetID).CountAsync();  
  35.                 if (check > 0)  
  36.                 {  
  37.                     ModelState.AddModelError(string.Empty, "Asset ID already exists");  
  38.                     return Page();  
  39.                 }  
  40.                 if (check == 0)  
  41.                 {  
  42.                     _context.WOAssetAssets.Add(newWOAA);  
  43.                     await _context.SaveChangesAsync();  
  44.                     return RedirectToPage("/WO/Asset/Index"new { id = newWOAA.WOALId });  
  45.                 }  
  46.             }  
  47.   
  48.             return null;  
  49.         }  
 the application is throwing an error message and halt. 
 
What should be the approach? 

Answers (1)