0

I am using ASP.Net and used EntityFramework to create my controller. In the controller there is the usual Delete and DeleteConfirmed actions. I don't ever want a user to actually delete a record but wouldn't mind that they think they deleted a record. My goal is to rework the delete confirmed record to change my RecordEntry table, IsValid column to 0 or false, meaning the record is marked as invalid rather than it being deleted.

This is the code for DeleteConfirmed. It has not yet been edited.

        // POST: RecordEntry/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var recordEntry = await _context.RecordEntry.FindAsync(id);
            _context.RecordEntry.Remove(recordEntry);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }
Three33Steele
  • 51
  • 1
  • 9
  • And what's the problem? What stops you from just editing the code? – Wiktor Zychla Aug 11 '21 at 16:09
  • @Wiktor Zychla I don't know how. I know I need to do _context.RecordEntry.Update instead of .Remove but I don't know what else – Three33Steele Aug 11 '21 at 16:11
  • Read the record out of the db by its id, update the property you want to update, save it back. See: https://learn.microsoft.com/en-us/ef/core/saving/basic – Jonathan Aug 11 '21 at 16:23

1 Answers1

1

When you select entry and then change it's values, you can already simply save changes to database.

// POST: RecordEntry/Delete/5 
[HttpPost, ActionName("Delete")] 
[ValidateAntiForgeryToken] 
public async Task<IActionResult> DeleteConfirmed(int id) { 
    var recordEntry = await _context.RecordEntry.FindAsync(id); // get entry
    recordEntry.IsValid = false; // Edit entry
    await _context.SaveChangesAsync(); // Save changes to db
    return RedirectToAction(nameof(Index)); // Return response
}

If you want to know more, please refer here (How to update record using Entity Framework 6?)

TDiblik
  • 522
  • 4
  • 18