public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } BoyLikes = await _context.BoyLikes.FindAsync(id); if (BoyLikes != null) { _context.BoyLikes.Remove(BoyLikes); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } BoyLikes = await _context.BoyLikes .Include(b => b.BoyPrefersNavigation) .Include(b => b.Boys).FirstOrDefaultAsync(m => m.BoyId == id); if (BoyLikes == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } BoyLikes = await _context.BoyLikes .Include(b => b.BoyPrefersNavigation) .Include(b => b.Boys).FirstOrDefaultAsync(m => m.BoyId == id); if (BoyLikes == null) { return(NotFound()); } ViewData["BoyPrefers"] = new SelectList(_context.Girl, "GirlId", "GirlId"); ViewData["BoyId"] = new SelectList(_context.Boy, "BoyId", "BoyId"); return(Page()); }
} //end of get //public async Task<IActionResult> OnPostAsync(int? id) public async Task <IActionResult> OnPostAsync(int?id, int[] PreferredGirls) //To Edit BoyPrefers data { if (!ModelState.IsValid) { return(Page()); } //var BoyToUpdate = await _context.Boy // Boy = await _context.Boy .Include(bl => bl.BoyLikes) .FirstOrDefaultAsync(m => m.BoyId == id); //past compatibility // Ilist<BoyLikes> bLikesList = new IList<BoyLikes>() ; // var bLikesListv = BoyToUpdate.BoyLikes; // OK // .ToListAsync(); // boySingleRelData.BoyLikesEnumerable = BoyToUpdate.BoyLikes; // dt 5-2-19 Err Runtime: NullReferenceException: Object reference not set to an instance of an object. // _context.Attach(Boy).State = EntityState.Modified; // Is reqd for data validation / integrity? /* * 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 <Boy>( BoyToUpdate, "Boy" //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 (PreferredGirls == null) //go back without doing anything. { return(Page()); } // remove old boylikes data /* for (int i = 0; i < bLikesListv.Count; i++) * { * //bLikesListv.Remove(i); * // var blTmp = bLikesListv[i].BoyId;// err: Cannot apply indexing with [] to an expression of type 'ICollection<BoyLikes> * // BoyLikes blTmp = _context.BoyLikes.Find(BoyToUpdate.BoyLikes.BoyId,BoyToUpdate.BoyLikes.BoyPrefers); //err: 'ICollection<BoyLikes>' does not contain a definition for 'BoyId' * // BoyToUpdate.BoyLikes.Remove(blTmp); * } */ foreach (var item in BoyToUpdate.BoyLikes) // 5-2-19 Err- Runtime: Collection was modified; enumeration operation may not execute. { // BoyToUpdate.BoyLikes.Remove(item); //.remove(); Runtime err _context.BoyLikes.Remove(item); } //if (BoyToUpdate.BoyLikes.Count>0) _context.SaveChangesAsync; // save only if old rec exists. // var PreferredGirlsHS = new HashSet<int>(PreferredGirls); // not usefull // int cnt = 0; // 1 //PreferredGirlsHS.count // var bl = new BoyLikes(); OK // PreferredGirls[0] = 1; //Just test then remove // PreferredGirls[1] = 2; // PreferredGirls[2] = 3; // PreferredGirls[3] = 4; //return NotFound("Test: PreferredGirls = " + PreferredGirls[2] ); //test //Dt 7-2-19. Find duplicate and return back if found. // Add for new entries, as we have already deleted old rec and, removed duplicates from new entries. 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' //var author = new Author{ FirstName = "William", LastName = "Shakespeare" }; var bl = new BoyLikes(); bl.BoyId = Boy.BoyId; //id; bl.BoyPrefers = PGirl; //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.BoyLikes.Add(bl); //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' } /* .cshtml * @foreach (var item in Model.Boy.BoyLikes) * { * <tr id="ForeachTR"> * <td></td> * <td> * <input type="text" name="PreferredGirls" value="@item.BoyPrefers" > * </td> * </tr> * } */ /* Likes = PreferredGirls; * int TotalPreferredGirls = Likes.count(); * // var item[] = BoyToUpdate.BoyBoyLikes.ToListAsync; * var item = BoyToUpdate.BoyBoyLikes; * for(cnt = 0; cnt > TotalPreferredGirls; cnt++) { * item[cnt] = PreferredGirls[cnt]; * } */ /* OK, but * foreach( var item in BoyToUpdate.BoyLikes) // 5-2-19 err: here we can not add more than the existing num of boylikes, e.g. 2 for boy(3) * // even worse: if num of (newly) entry is less e.g. 1 then err of index outseide the bound * // Soln: For Loop: w.r.t. if cnt > PreferredGirls.count() ? No * // -- Use this loop to remove old data. We are not able to edit data with select-box, as can be done for new entry. * { * //if cnt > PreferredGirls.count() * // validate if girl-code exists 1-2-19 nsb, can be done in javascript * * //Dt 5-2-19Err: IndexOutOfRangeException: Index was outside the bounds of the array. * // OnPostAsync(Nullable<int> id, int[] PreferredGirls) in EditWithLikes.cshtml..cs, * if (GirlExists(PreferredGirls[cnt])) { * item.BoyPrefers = PreferredGirls[cnt]; * cnt++; * } * else{ * return NotFound("Error! Your Preferred Reg. num " + PreferredGirls[cnt] + " does not exists! Pl Go back and correct." ); * } * } */ } 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