using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using CertAuth.Data; using CertAuth.Domain; using Microsoft.EntityFrameworkCore.Infrastructure; using Newtonsoft.Json; using CertAuthAdmin.Controllers; using DevExtreme.AspNet.Mvc; using System.Security.Cryptography.X509Certificates; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Data.SqlClient; using System.Security.Cryptography.Xml; namespace CertAuthAdmin.Pages.Users { public class EditModel : PageModel { private readonly CertAuth.Data.CertAuthContext _context; private readonly Models.TableDtlDA TDCtrlr; //private Models.UsersDA UDA; public EditModel(CertAuth.Data.CertAuthContext context) { _context = context; TDCtrlr = new Models.TableDtlDA(); //UDA = new Models.UsersDA(); } [BindProperty] public User user { get; set; } [BindProperty(Name = "editID", SupportsGet = true)] public string editID { get; set; } public List tblDtl { get; set; } public SelectList slUserType { get; set; } public List sliUType{ get; set; } public SelectList slOrganizations { get; set; } public List sliOrg { get; set; } public SelectList slEmpSecuity { get; set; } public List sliEmpSec { get; set; } public SelectList slStatus { get; set; } public List sliStatus { get; set; } public async Task OnGetAsync(string id) { //if (id == null) //{ // return NotFound(); //} if (id == null && editID != null) { id = editID; } user = await _context.Users.FirstOrDefaultAsync(m => m.UserID == id); if (user != null) { LoadLists(); } //if (user == null) //{ // return NotFound(); //} return Page(); } // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task OnPostAsync() { if (!ModelState.IsValid) { LoadLists(); return Page(); } //User user1 = user; user.TimeStamp = DateTime.Now; _context.Attach(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(user.UserID)) { LoadLists(); return NotFound(); } else { LoadLists(); throw; } } return RedirectToPage("./UsersList"); } private bool UserExists(string id) { return _context.Users.Any(e => e.UserID == id); } private void LoadLists() { bool slct = false; slStatus = new SelectList(TDCtrlr.GetSlctItms("Status", "A", true, ""), "Value", "Text"); slUserType = new SelectList(TDCtrlr.GetSlctItms("UserType", "A", true, ""), "Value", "Text"); slOrganizations = new SelectList(TDCtrlr.GetSlctItms("Organization", "A", true, ""), "Value", "Text"); slEmpSecuity = new SelectList(TDCtrlr.GetSlctItms("EmpSecurity", "A", true, ""), "Value", "Text"); } //private void LoadLists() //{ // bool slct = false; // tblDtl = TDCtrlr.GetRecrdByTableCode("UserType", "Active"); // sliUType = new List(); // foreach (var itm in tblDtl) // { // if (user.UserType == itm.DetailCode) // { slct = true; } // sliUType.Add(new SelectListItem() // { // Value = itm.DetailCode, // Text = itm.CodeDescShort, // Selected = slct // }); // slct = false; // }; // slUserType = new SelectList(sliUType, "Value", "Text"); // //slOrganizations = new SelectList(TDCtrlr.GetByTableCode("Organization", "Active")); // slct = false; // tblDtl = TDCtrlr.GetRecrdByTableCode("Organization", "Active"); // sliOrg = new List(); // foreach (var itm in tblDtl) // { // if (user.OrganizationID == itm.DetailCode) // { slct = true; } // sliOrg.Add(new SelectListItem() // { // Value = itm.DetailCode, // Text = itm.CodeDescShort, // Selected = slct // }); // slct = false; // }; // slOrganizations = new SelectList(sliOrg, "Value", "Text"); // //Employee Security // slct = false; // tblDtl = TDCtrlr.GetRecrdByTableCode("EmpSecurity", "Active"); // sliEmpSec = new List(); // foreach (var itm in tblDtl) // { // if (user.EmployeeSecurity == itm.DetailCode) // { slct = true; } // sliEmpSec.Add(new SelectListItem() // { // Value = itm.DetailCode, // Text = itm.CodeDescShort, // Selected = slct // }); // slct = false; // }; // slEmpSecuity = new SelectList(sliEmpSec, "Value", "Text"); // //Status // slct = false; // tblDtl = TDCtrlr.GetRecrdByTableCode("Status", "Active"); // sliStatus = new List(); // foreach (var itm in tblDtl) // { // if (user.Status == itm.DetailCode) // { slct = true; } // sliStatus.Add(new SelectListItem() // { // Value = itm.DetailCode, // Text = itm.CodeDescShort, // Selected = slct // }); // slct = false; // }; // slStatus = new SelectList(sliStatus, "Value", "Text"); //} } }