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 System.ComponentModel.DataAnnotations; using CertAuth.Data; using CertAuth.Domain; namespace CertAuthAdmin.Pages.VendorMstr { public class VendorEditModel : PageModel { private readonly CertAuth.Data.CertAuthContext _context; private readonly Models.TableDtlDA TDCtrlr; public VendorEditModel(CertAuth.Data.CertAuthContext context) { _context = context; TDCtrlr = new Models.TableDtlDA(); } [BindProperty] public CertAuth.Domain.VendorMstr VendorMstr { get; set; } [BindProperty(Name = "editID", SupportsGet = true)] public int editID { get; set; } // added 12/1/2020 //[BindProperty] public CertAuth.Domain.User _user { get; set; } [BindProperty(Name = "UserID")] [Required] [Remote(action: "VerifyUserID", controller: "Users")] public string UserID { get; set; } public SelectList slStatus { get; set; } public SelectList slPhone { get; set; } public SelectList slState { get; set; } public SelectList slCountry { get; set; } public SelectList slCredRate { get; set; } public async Task OnGetAsync(int? id) { if (id == null && editID == 0) { //return NotFound(); } if (id == null && editID != 0) { id = editID; } VendorMstr = await _context.VendorMstr.FirstOrDefaultAsync(m => m.Id == id); if (VendorMstr == null) { //return NotFound(); } _user = await _context.Users.FirstOrDefaultAsync(u => u.VendorKey == VendorMstr.Id); if (_user != null && !string.IsNullOrEmpty(_user.UserID)) { UserID = _user.UserID; } LoadLists(); 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(); } if (!string.IsNullOrEmpty(UserID)) { //Check if UserID exists & is not already a vendor (VendorKey <> 0 or UserType = 'Vndr') // If the userId is OK, then Update the User table with the vendorKey & userType. try { _user = await _context.Users.FirstOrDefaultAsync(u => u.UserID == UserID); } catch(Exception ex) { } } VendorMstr.TimeStamp = DateTime.Now; _context.Attach(VendorMstr).State = EntityState.Modified; // added following try-catch to correct bug on saving changes to vendor master edit screen //try //{ // await _context.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ //} try { _user.VendorKey = VendorMstr.Id; _user.UserType = "Vndr"; _context.Attach(_user).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VendorMstrExists(VendorMstr.Id)) { return NotFound(); } else { throw; } } return RedirectToPage("./VendorList"); } private bool VendorMstrExists(int id) { return _context.VendorMstr.Any(e => e.Id == id); } private void LoadLists() { slStatus = new SelectList(TDCtrlr.GetSlctItms("Status", "A", true, ""), "Value", "Text"); slPhone = new SelectList(TDCtrlr.GetSlctItms("phonetype", "A", true, ""), "Value", "Text"); slState = new SelectList(TDCtrlr.GetSlctItms("ST", "A", true, ""), "Value", "Text"); slCountry = new SelectList(TDCtrlr.GetSlctItms("Cntry", "A", true, ""), "Value", "Text"); slCredRate = new SelectList(TDCtrlr.GetSlctItms("CR", "A", true, ""), "Value", "Text"); } } }