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 CertAuth.Data; using CertAuth.Domain; using Microsoft.EntityFrameworkCore; using System.Xml; using CertAuthAdmin.Models; using System.ComponentModel.DataAnnotations; namespace CertAuthAdmin.Pages.VendorMstr { public class AddVendorModel : PageModel { private readonly CertAuth.Data.CertAuthContext _context; private readonly Models.TableDtlDA TDCtrlr; public AddVendorModel(CertAuth.Data.CertAuthContext context) { _context = context; TDCtrlr = new Models.TableDtlDA(); } // added 12/1/2020 public CertAuth.Domain.User _user { get; set; } [BindProperty(Name = "UserID")] [Required] [Remote(action: "VerifyUserID", controller: "Users")] public string UserID { get; set; } //public IActionResult OnGet() //{ // return Page(); //} [BindProperty] public CertAuth.Domain.VendorMstr VendorMstr { 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() { //slStatus = new SelectList(TDCtrlr.GetByTableCode("Status", "Active")); LoadLists(); } // 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.Added; _context.VendorMstr.Add(VendorMstr); try { await _context.SaveChangesAsync(); _user.VendorKey = VendorMstr.Id; _user.UserType = "Vndr"; _context.Attach(_user).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (VendorExists(VendorMstr.Id)) { return BadRequest(); } } return RedirectToPage("./VendorList"); } private bool VendorExists(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"); } } }