Created
October 22, 2017 04:43
-
-
Save A2H111/35af289938eae9b6b99fe0644721380c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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