// GET: Buyer public ActionResult Index() { var userId = User.Identity.Name; var userData = UserManager.Users.Where(x => x.UserName == userId).SingleOrDefault(); var empData = db.Employee.Where(x => x.EmployeeID == userId).SingleOrDefault(); var companyData = db.CompanyInfo.Where(x => x.CompanyCode == empData.CompanyCode).SingleOrDefault(); Buyer_parent m = new Buyer_parent(); BuyerCompInfoViewModel info = new BuyerCompInfoViewModel(); BuyerSettingViewModel buyer = new BuyerSettingViewModel(); info.CompanyName = companyData.CompanyName; info.TaxID = companyData.TaxID; info.Tel = companyData.Tel; info.Address = companyData.Address; info.Email = companyData.Email; buyer.EmployeeID = User.Identity.Name; buyer.Name = userData.RealName; buyer.Email = userData.Email; buyer.Tel = empData.Tel; buyer.Mobile = empData.Mobile; buyer.EnableTwoFactorAuth = userData.TwoFactorEnabled; m.BuyerCompInfoViewModel = info; m.BuyerSettingViewModel = buyer; return(View(m)); }
public async Task <ActionResult> Index(Buyer_parent m) { try { //m.BuyerSettingViewModel.EmployeeID = User.Identity.Name; var userId = User.Identity.Name; var userData = UserManager.Users.Where(x => x.UserName == userId).SingleOrDefault(); var empData = db.Employee.Where(x => x.EmployeeID == userId).SingleOrDefault(); var companyData = db.CompanyInfo.Where(x => x.CompanyCode == empData.CompanyCode).SingleOrDefault(); BuyerCompInfoViewModel info = new BuyerCompInfoViewModel(); BuyerSettingViewModel buyer = new BuyerSettingViewModel(); //姓名 userData.RealName = m.BuyerSettingViewModel.Name; empData.Name = m.BuyerSettingViewModel.Name; //信箱 string holder = userData.Email; userData.Email = m.BuyerSettingViewModel.Email; empData.Email = m.BuyerSettingViewModel.Email; //如果信箱有改,驗證要重置為false if (holder != m.BuyerSettingViewModel.Email) { userData.EmailConfirmed = false; } //手機 userData.PhoneNumber = m.BuyerSettingViewModel.Mobile; empData.Mobile = m.BuyerSettingViewModel.Mobile; //市話 empData.Tel = m.BuyerSettingViewModel.Tel; //雙因素 //判斷Email是否通過驗證 userData.TwoFactorEnabled = m.BuyerSettingViewModel.EnableTwoFactorAuth; var r = await UserManager.UpdateAsync(userData); db.Entry(empData).State = System.Data.Entity.EntityState.Modified; var r2 = await db.SaveChangesAsync(); if (r.Succeeded && r2 > 0) { TempData["SuccessMsg"] = "修改成功!"; return(RedirectToAction("Index")); } TempData["ErrorMsg"] = "修改失敗,請檢查網路連線再試一次。"; return(RedirectToAction("Index")); } catch { TempData["ErrorMsg"] = "修改失敗,不可空值,並請檢查網路連線再試一次..."; return(RedirectToAction("Index")); } }