Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@A2H111
Created October 22, 2017 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A2H111/35af289938eae9b6b99fe0644721380c to your computer and use it in GitHub Desktop.
Save A2H111/35af289938eae9b6b99fe0644721380c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
namespace NetCoreRazorPageApp.Pages.Employee
{
public class DeletePageModel : PageModel
{
public readonly NetCoreRazorPageApp.Model.EmployeeDbContext _dbContext;
public DeletePageModel(NetCoreRazorPageApp.Model.EmployeeDbContext dbContext)
{
_dbContext = dbContext;
}
[BindProperty]
public NetCoreRazorPageApp.Model.Employee Employee { get; set; }
//Method to retreive the selected record details
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
//Get the value from database based on ID
Employee = await _dbContext.Employee.FirstOrDefaultAsync(i => i.EmployeeID == id);
if (Employee == null)
{
return NotFound();
}
return Page();
}
//Method to Delete record from database
public async Task<IActionResult> OnPostAsync(int? id)
{
if (id == null)
{
return NotFound();
}
//Delete Employee details to database
Employee = await _dbContext.Employee.FindAsync(id);
if (Employee != null)
{
_dbContext.Employee.Remove(Employee);
await _dbContext.SaveChangesAsync();
}
//Redirecting back to Index page after successfull Delete operation
return RedirectToPage("/Index");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment