public ActionResult New(HttpPostedFileWrapper[] fileup)//String[] Ins, String[] Deg, String[] Year) { EmployeeBasicInfoVM emp = null; var empbasic = new EmployeeBasicInfo(); empbasic.FirstName = emp.FirstName; empbasic.FatherName = emp.FatherName; empbasic.SurName = emp.SurName; empbasic.Gender = emp.Gender; empbasic.DateOfBirth = emp.DateOfBirth; empbasic.CNIC = emp.CNIC; empbasic.Religion = emp.Religion; empbasic.Domicile = emp.Domicile; empbasic.Address = emp.Address; empbasic.State = emp.State; empbasic.Country = emp.Country; empbasic.PostalCode = emp.PostalCode; empbasic.HomePhone = emp.HomePhone; empbasic.MobilePhone = emp.MobilePhone; empbasic.EmailAddress = emp.EmailAddress; unitOfWork.Save(); //for (int i = 0; i < Ins.Length; i++) //{ // var institute = Ins[i]; // var degree = Deg[i]; // var year = Year[i]; // Logging.ExecuteSafely<EmployeeVM>(() => // { // var emp = new Employee(); // emp.Name = "Owais"; // emp.Age = 100; // return null; // }); //} //List<Country> countrylist = new List<Country>(); //countrylist.Add(new Country { Id = 1, Name = "Owais" }); //countrylist.Add(new Country { Id = 2, Name = "ALi" }); //countrylist.Add(new Country { Id = 3, Name = "Ahmed" }); //countrylist.Add(new Country { Id = 4, Name = "John" }); //countrylist.Add(new Country { Id = 5, Name = "Smith" }); return(View()); }
public ActionResult Create(EmployeeBasicInfo employee) { using (var dbTransaction = _db.Database.BeginTransaction()) { const string basePath = @"/Images/Employees/Profile/"; var filePath = ""; try { ModelState.Clear(); employee.EmployeeId = string.Format("EI-{0:000000}", _db.EmployeeBasicInfos.Count() + 1); employee.EntryBy = _db.Users.First(x => x.UserName == User.Identity.Name).Id; employee.EntryDate = DateTime.Now; employee.Status = Status.Active; var serverPath = Server.MapPath("~" + basePath); var profilePhoto = Request.Files["ImageUrl"]; // ReSharper disable once PossibleNullReferenceException if (profilePhoto.ContentLength > 0) { // 1048567 bytes = 1 MegaByte if (profilePhoto.FileName == string.Empty || profilePhoto.ContentLength > 2097152) { ModelState.AddModelError("ImageUrl", @"Max file size: 2 MB!"); return(View(employee)); } var extension = Path.GetExtension(profilePhoto.FileName); if (extension == null) { ModelState.AddModelError("ImageUrl", @"Its not a valid file."); return(View(employee)); } extension = extension.ToLower(); if (_allowedLogoFileTypes.IndexOf(extension) == -1) { ModelState.AddModelError("ImageUrl", @"Only .jpg, .png, .jpeg, .gif, .bmp file types are allowed."); return(View(employee)); } var image = Image.FromStream(profilePhoto.InputStream); if (image.Width != image.Height) { ModelState.AddModelError("ImageUrl", @"Image Should be square size."); return(View(employee)); } var generatedFileName = string.Format("{0}_{1}", employee.EmployeeId, profilePhoto.FileName); filePath = Path.Combine(serverPath, generatedFileName); if (!Directory.Exists(serverPath)) { Directory.CreateDirectory(serverPath); } profilePhoto.SaveAs(filePath); employee.ImageUrl = string.Format("{0}{1}", basePath, generatedFileName); } TryValidateModel(employee); if (ModelState.IsValid) { _db.EmployeeBasicInfos.Add(employee); _db.SaveChanges(); dbTransaction.Commit(); TempData["Toastr"] = Toastr.Added; return(RedirectToAction("Index")); } dbTransaction.Rollback(); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } return(View(employee)); } catch (Exception ex) { dbTransaction.Rollback(); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } TempData["Toastr"] = Toastr.DbError(ex.Message); return(RedirectToAction("Index")); } finally { ViewBag.Designations = new SelectList(_db.EmployeeDesignations, "Id", "DesignationTitleEn"); ViewBag.BloodGroups = Common.ToSelectList <BloodGroup>(); ViewBag.Levels = Common.ToSelectList <EmployeeLevel>(); } } }