public ActionResult Create([Bind(Include = "CustomerId,TrebId,WebsiteUrl,ApplicationID,Password,CompanyID,UserId,PhotoPath,FirstName,LastName,MiddleName,EmailID,MobileNo,CountryID,StateID,CityID,ZipCode,Latitude,Longitude,CreatedOn,LastUpdatedOn,MobileVerifyCode,EmailVerifyCode,IsMobileVerified,IsEmailVerified,IsActive,AdminCompanyAddress")] AdminModel AdminModel, HttpPostedFileBase file, HttpPostedFileBase Logo) { TempData["ShowMessage"] = ""; TempData["MessageBody"] = ""; UserPermissionAction("admin", RoleAction.create.ToString()); CheckPermission(); Mapper.CreateMap <CommunicationApp.Models.AdminModel, CommunicationApp.Entity.Customer>(); CommunicationApp.Entity.Customer Customer = Mapper.Map <CommunicationApp.Models.AdminModel, CommunicationApp.Entity.Customer>(AdminModel); if (ModelState.IsValid) { var customerFound = _CustomerService.GetCustomers().Where(x => x.EmailId == Customer.EmailId || x.MobileNo == Customer.MobileNo || x.WebsiteUrl == Customer.WebsiteUrl).FirstOrDefault(); if (customerFound == null) { //Save Company of admin Company Company = new Entity.Company(); Company.WebSite = Customer.WebsiteUrl; Company.CountryID = 1; Company.StateID = 1; Company.CityID = 1; Company.CompanyAddress = AdminModel.AdminCompanyAddress != "" ? AdminModel.AdminCompanyAddress : "*****@*****.**"; Company.CompanyName = AdminModel.CompanyName != null ? AdminModel.CompanyName : "Company"; Company.EmailID = "*****@*****.**"; Company.PhoneNo = "123456987"; Company.IsActive = true; if (Logo != null) { Company.LogoPath = Savefile(Logo); } _CompanyService.InsertCompany(Company); //Insert User first CommunicationApp.Entity.User user = new CommunicationApp.Entity.User(); //user.UserId =0; //New Case user.FirstName = Customer.FirstName; user.TrebId = Customer.TrebId; user.LastName = Customer.LastName; user.UserName = Customer.EmailId; user.Password = SecurityFunction.EncryptString(AdminModel.Password); user.UserEmailAddress = Customer.EmailId; user.CompanyID = Company.CompanyID; user.CreatedOn = DateTime.Now; user.LastUpdatedOn = DateTime.Now; user.TrebId = "0000000"; user.IsActive = true; _UserService.InsertUser(user); //End : Insert User first var UserID = user.UserId; if (user.UserId > 0) { //Insert User Role CommunicationApp.Entity.UserRole userRole = new CommunicationApp.Entity.UserRole(); userRole.UserId = user.UserId; userRole.RoleId = 2; //By Default set new Admin/user role id=2 _UserRoleService.InsertUserRole(userRole); //End : Insert User Role //Insert the Customer Customer.FirstName = Customer.FirstName; Customer.UserId = user.UserId; Customer.Designation = "Admin"; Customer.MobileVerifyCode = CommonCls.GetNumericCode(); Customer.EmailVerifyCode = CommonCls.GetNumericCode(); Customer.MobileVerifyCode = "9999"; Customer.EmailVerifyCode = "9999"; Customer.CreatedOn = DateTime.Now; Customer.CompanyID = Company.CompanyID; Customer.Address = ""; Customer.ZipCode = ""; Customer.IsEmailVerified = true; var PhotoPath = ""; if (file != null) { PhotoPath = Savefile(file); } Customer.PhotoPath = PhotoPath; Customer.IsMobileVerified = false; Customer.ApplicationId = AdminModel.ApplicationId; Customer.DeviceSerialNo = AdminModel.DeviceSerialNo; Customer.DeviceType = AdminModel.DeviceType; Customer.IsUpdated = false; Customer.IsNotificationSoundOn = true; if (Customer.FirstName == null) { Customer.FirstName = ""; } if (Customer.LastName == null) { Customer.LastName = ""; } if (Customer.MiddleName == null) { Customer.MiddleName = ""; } _CustomerService.InsertCustomer(Customer); var CustomerID = Customer.CustomerId.ToString(); AdminModel.CustomerId = Customer.CustomerId; TempData["ShowMessage"] = "Success"; TempData["MessageBody"] = "Admin successfully register."; } } else { if (customerFound.EmailId == AdminModel.EmailID) { TempData["ShowMessage"] = "Error"; TempData["MessageBody"] = "Email is already exist."; } else if (customerFound.MobileNo == AdminModel.MobileNo) { TempData["ShowMessage"] = "Error"; TempData["MessageBody"] = "MobileNos is already exist."; } else { TempData["ShowMessage"] = "Error"; TempData["MessageBody"] = "Some error occured."; } } } else { var errors = ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => new { x.Key, x.Value.Errors }).ToArray(); var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors); TempData["ShowMessage"] = "Error"; TempData["MessageBody"] = "Please fill the required data."; return(View(AdminModel)); } return(RedirectToAction("Index")); }