Skip to content

Instantly share code, notes, and snippets.

@A2H111
Created October 22, 2017 04:35
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/88ca083ba1750a60592cfebf08e1cc9e to your computer and use it in GitHub Desktop.
Save A2H111/88ca083ba1750a60592cfebf08e1cc9e 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
{
public class EditPageModel : PageModel
{
//Creating dbContext object
public readonly NetCoreRazorPageApp.Model.EmployeeDbContext _dbContext;
public EditPageModel(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 save the data back to database
public async Task<IActionResult> OnPostAsync()
{
try
{
//Updating modified Employee details to database
_dbContext.Attach(Employee).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
throw ex;
}
//Redirecting back to Index page after successfull save
return RedirectToPage("./Index");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment