public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } GirlLikes = await _context.GirlLikes .Include(g => g.GirlPrefersNavigation) .Include(g => g.Girls).FirstOrDefaultAsync(m => m.GirlId == id); if (GirlLikes == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } GirlLikes = await _context.GirlLikes.FindAsync(id); if (GirlLikes != null) { _context.GirlLikes.Remove(GirlLikes); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } GirlLikes = await _context.GirlLikes .Include(g => g.GirlPrefersNavigation) .Include(g => g.Girls).FirstOrDefaultAsync(m => m.GirlId == id); if (GirlLikes == null) { return(NotFound()); } ViewData["GirlPrefers"] = new SelectList(_context.Boy, "BoyId", "BoyId"); ViewData["GirlId"] = new SelectList(_context.Girl, "GirlId", "GirlId"); return(Page()); }
} //end of get //public async Task<IActionResult> OnPostAsync(int? id) public async Task <IActionResult> OnPostAsync(int?id, int[] PreferredBoys) //To Edit BoyPrefers data { if (!ModelState.IsValid) { return(Page()); } var GirlToUpdate = await _context.Girl // Boy = await _context.Boy .Include(gl => gl.GirlLikes) .FirstOrDefaultAsync(m => m.GirlId == id); //past compatibility // _context.Attach(Boy).State = EntityState.Modified; /* * The instance of entity type 'Boy' cannot be tracked because another instance with the same key value for {'BoyId'} is already being tracked. * When attaching existing entities, ensure that only one entity instance with a given key value is attached. * * Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values. */ // _context.Attach(Boy).State = EntityState.Modified; // _context.Attach(BoyToUpdate).State = EntityState.Modified; // _context.Attach(Boy.BoyLikes).State = EntityState.Modified; //nsb 24-1-19 //err-runtime: The entity type 'HashSet<BoyLikes>' was not found. // Though Err-message, data is saved?, No , ( Can ignore/bypass err-msg? ) // BoyToUpdate.BoyId = Boy.BoyId; // Optional, not reqd // BoyToUpdate.Name = Boy.Name ; BoyToUpdate.MobileNum = Boy.MobileNum; //keep ok //BoyToUpdate.BoyLikes if (await TryUpdateModelAsync <Girl>( GirlToUpdate, "Girl" //not Boy but "Boy" reqd. , All parent data saved/ updated. OK keep. //, "Boy" //, i => i.BoyId, i => i.Name, i => i.MobileNum // , i => i.Boy.Name, i => i.Boy.MobileNum //OK but can be auto. //,BoyToUpdate.Name = Boy.Name, BoyToUpdate.MobileNum = Boy.MobileNum )) { //Update BoyBoyLikes BoyPrefers data if (PreferredBoys == null) //go back without doing anything. { return(Page()); } // var PreferredGirlsHS = new HashSet<int>(PreferredGirls); // int cnt = 0; // 1 //PreferredGirlsHS.count /* * foreach (var PGirl in PreferredGirls) * { * // BoyLikes.Find(1).BoyPrefers = PGirl; * // BoyToUpdate.BoyLikes.Find(1).BoyPrefers = PGirl; //Err: 'ICollection<BoyLikes>' does not contain a definition for 'Find' * //cnt++; * } */ foreach (var item in GirlToUpdate.GirlLikes) { //if cnt > PreferredGirls.count() // item.BoyPrefers = PreferredGirls[cnt]; // cnt++; _context.GirlLikes.Remove(item); } foreach (var PBoy in PreferredBoys) { // BoyLikes.Find(1).BoyPrefers = PGirl; // BoyToUpdate.BoyLikes.Find(1).BoyPrefers = PGirl; //Err: 'ICollection<BoyLikes>' does not contain a definition for 'Find' //var author = new Author{ FirstName = "William", LastName = "Shakespeare" }; var gl = new GirlLikes(); gl.GirlId = Girl.GirlId; //id; gl.GirlPrefers = PBoy; //bl.BoyLikes.ID = // BoyToUpdate.BoyLikes.add(new BoyLikes{BoyToUpdate.BoyId, PGirl}); //Err: 'ICollection<BoyLikes>' does not contain a definition for 'Find' //BoyToUpdate.BoyLikes.add(bl); //err: 'ICollection<BoyLikes>' does not contain a definition for 'add' // bLikesListv.Add(bl); // OK. WARNING .add instead of .Add wasted lot of time ! // Var never used ! _context.GirlLikes.Add(gl); //OK // boySingleRelData.BoyLikesEnumerable.Add(bl); //err: 'IEnumerable<BoyLikes>' does not contain a definition for 'Add' //cnt++; //return NotFound("Test: PreferredGirls 1st val = " + bl.BoyPrefers ); //test // await _context.SaveChangesAsync(); // Dt 7-2-19 Err: Duplicate entry '20' for key 'PRIMARY' } } else //return NoContent(); //return NotFound(); { } await _context.SaveChangesAsync(); /* * try * { * await _context.SaveChangesAsync(); * } * catch (DbUpdateConcurrencyException) * { * if (!BoyExists(Boy.BoyId)) * { * return NotFound(); * } * else * { * throw; * } * } */ return(RedirectToPage("./Index")); } //end of post