public async Task <IActionResult> Register(Registration model) { try { if (ModelState.IsValid) { if (_context.Registration.Any(name => name.Email.Equals(model.Email))) { ModelState.AddModelError(string.Empty, "User is already exists"); } else { _context.Add(model); await _context.SaveChangesAsync(); HttpContext.Session.SetString(SessionEmpName, model.Email); HttpContext.Session.SetString(SessionISRole, model.Role); HttpContext.Session.SetString(SessionIsLogin, "1"); return(RedirectToAction("Index", "Department")); } } } catch (Exception ex) { string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message); message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty)); ModelState.AddModelError(string.Empty, message); } return(View(model)); }
public async Task <IActionResult> Create([Bind("Department_Id,Department_Name")] DepartmentMaster departmentMaster) { try { if (ModelState.IsValid) { if (_context.DepartmentMaster.Any(name => name.Department_Name.Equals(departmentMaster.Department_Name) && name.IsDelete == 0)) { ModelState.AddModelError(string.Empty, "Department is already exists"); } else { departmentMaster.IsDelete = 0; _context.Add(departmentMaster); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } catch (Exception ex) { string message = string.Format("<b>Message:</b> {0}<br /><br />", ex.Message); message += string.Format("<b>StackTrace:</b> {0}<br /><br />", ex.StackTrace.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>Source:</b> {0}<br /><br />", ex.Source.Replace(Environment.NewLine, string.Empty)); message += string.Format("<b>TargetSite:</b> {0}", ex.TargetSite.ToString().Replace(Environment.NewLine, string.Empty)); ModelState.AddModelError(string.Empty, message); } return(View(departmentMaster)); }
public async Task <IActionResult> Create([Bind("Employee_Id,Employee_Name,Department_Id,JoiningDate,Address,IsDelete,Email,MobileNo,Image")] EmployeeMaster employeeMaster) { if (ModelState.IsValid) { if (_context.EmployeeMaster.Any(name => name.Email.Equals(employeeMaster.Email) && name.IsDelete == 0)) { ModelState.AddModelError(string.Empty, "Employee is already exists"); var DepartmentLists = _context.DepartmentMaster.Where(s => s.IsDelete == 0).ToList(); ViewBag.Department = new SelectList(DepartmentLists, "Department_Id", "Department_Name"); return(View(employeeMaster)); } else { var files = HttpContext.Request.Form.Files; foreach (var Image in files) { if (Image != null && Image.Length > 0) { var supportedTypes = new[] { "jpg", "jpeg", "png", "gif", "bmp" }; var fileExt = System.IO.Path.GetExtension(Image.FileName).Substring(1); if (!supportedTypes.Contains(fileExt)) { string ErrorMessage = "File Extension Is InValid - Only Upload Image File"; ModelState.AddModelError(string.Empty, ErrorMessage); var DepartmentLists = _context.DepartmentMaster.Where(s => s.IsDelete == 0).ToList(); ViewBag.Department = new SelectList(DepartmentLists, "Department_Id", "Department_Name"); return(View(employeeMaster)); } var file = Image; var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "Emp_IMg"); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse (file.ContentDisposition).FileName.ToString().Trim('"'); System.Console.WriteLine(fileName); using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create)) { await file.CopyToAsync(fileStream); employeeMaster.Image = file.FileName; } } } } //employeeMaster.Department_Id = 2; employeeMaster.IsDelete = 0; _context.Add(employeeMaster); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } return(View(employeeMaster)); }