示例#1
0
        public ActionResult UserGridViewPartialAddNew([ModelBinder(typeof(DevExpressEditorsBinder))] Qiyas.BusinessLogicLayer.Entity.Persons.Person item)
        {
            if (ModelState.IsValid)
            {
                string firstName  = "";
                string middleName = "";
                string lastName   = "";
                GetFullNameFromDisplayName(item.DisplayName, out firstName, out middleName, out lastName);
                var user = new BusinessLogicLayer.Entity.Persons.Person {
                    UserName = item.UserName, Email = item.Email, IsActive = item.IsActive, DisplayName = item.DisplayName.Trim(), FirstName = firstName, MiddleName = middleName, LastName = lastName, CreatedDate = DateTime.Now, ModifiedDate = DateTime.Now
                };
                try
                {
                    // Insert here a code to insert the new item in your model
                    bool usernameExists = UserNameExists(item.UserName, 0);
                    bool emailExists    = EmailExists(item.Email, 0);

                    if (!usernameExists && !emailExists)
                    {
                        var result = UserManager.Create <BusinessLogicLayer.Entity.Persons.Person, string>(user, item.Password);
                        if (result.Succeeded)
                        {
                        }
                        else
                        {
                            string msgError = "";

                            foreach (var error in result.Errors)
                            {
                                if (error.Contains("Passwords"))
                                {
                                    msgError = Resources.MainResource.PasswordCheckError + " ";
                                }
                                else
                                {
                                    msgError += error + " ";
                                }
                            }
                            ViewData["EditError"] = msgError;
                        }
                    }
                    else if (usernameExists)
                    {
                        ViewData["EditError"] = Resources.MainResource.UserNameExists;
                    }
                    else if (emailExists)
                    {
                        ViewData["EditError"] = Resources.MainResource.EmailExists;
                    }
                }
                catch (Exception e)
                {
                    if (e.Message.Contains("null") && e.Message.Contains("password"))
                    {
                        ViewData["EditError"] = Resources.MainResource.RequiredPassword;
                    }
                    else
                    {
                        ViewData["EditError"] = e.Message;
                    }
                }
            }
            else
            {
                string msgError = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        msgError += error.ErrorMessage + " ";
                    }
                }
                ViewData["EditError"] = msgError;
            }
            var model = new BusinessLogicLayer.Components.Persons.PersonLogic().GetAll();

            return(PartialView("_UserGridViewPartial", model));
        }
示例#2
0
        public ActionResult UserGridViewPartialUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] Qiyas.BusinessLogicLayer.Entity.Persons.Person item)
        {
            if (ModelState.IsValid)
            {
                string firstName  = "";
                string middleName = "";
                string lastName   = "";
                GetFullNameFromDisplayName(item.DisplayName, out firstName, out middleName, out lastName);
                var user = new BusinessLogicLayer.Entity.Persons.Person(item.BusinessEntityId);
                if (user != null)
                {
                    user.UserName     = item.UserName;
                    user.Email        = item.Email;
                    user.IsActive     = item.IsActive;
                    user.DisplayName  = item.DisplayName.Trim();
                    user.FirstName    = firstName;
                    user.MiddleName   = middleName;
                    user.LastName     = lastName;
                    user.ModifiedDate = DateTime.Now;

                    try
                    {
                        // Insert here a code to insert the new item in your model
                        bool usernameExists = UserNameExists(item.UserName, user.BusinessEntityId);
                        bool emailExists    = EmailExists(item.Email, user.BusinessEntityId);

                        if (!usernameExists && !emailExists)
                        {
                            bool result = user.Save();
                            if (!user.CurrentCredential.IsNew)
                            {
                                user.CurrentCredential.Save();
                            }

                            if (result)
                            {
                                if (!string.IsNullOrEmpty(item.Password))
                                {
                                    string code       = UserManager.GeneratePasswordResetToken(user.Id);
                                    var    passresult = UserManager.ResetPassword(user.Id, code, item.Password);
                                    if (passresult.Succeeded)
                                    {
                                    }
                                    else
                                    {
                                        string msgError = "";

                                        foreach (var error in passresult.Errors)
                                        {
                                            if (error.Contains("Passwords"))
                                            {
                                                msgError = Resources.MainResource.PasswordCheckError + " ";
                                            }
                                            else
                                            {
                                                msgError += error + " ";
                                            }
                                        }
                                        ViewData["EditError"] = msgError;
                                    }
                                }
                            }
                        }
                        else if (usernameExists)
                        {
                            ViewData["EditError"] = Resources.MainResource.UserNameExists;
                        }
                        else if (emailExists)
                        {
                            ViewData["EditError"] = Resources.MainResource.EmailExists;
                        }
                    }
                    catch (Exception e)
                    {
                        if (e.Message.Contains("null") && e.Message.Contains("password"))
                        {
                            ViewData["EditError"] = Resources.MainResource.RequiredPassword;
                        }
                        else
                        {
                            ViewData["EditError"] = e.Message;
                        }
                    }
                }
                else
                {
                    ViewData["EditError"] = Resources.MainResource.NoUserError;
                }
            }
            else
            {
                string msgError = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        msgError += error.ErrorMessage + " ";
                    }
                }
                ViewData["EditError"] = msgError;
            }

            var model = new BusinessLogicLayer.Components.Persons.PersonLogic().GetAll();

            return(PartialView("_UserGridViewPartial", model));
        }