コード例 #1
0
        public ActionResult Create(Member user)
        {
            Member savedUser = null;

            if (ModelState.IsValid)
            {
                try
                {
                    //db.User.Add(user);
                    authentication.CreateUserAndAccount(user);
                    savedUser = db.Member.FirstOrDefault(x => x.UserName == user.UserName);
                    if (savedUser != null)
                    {
                        savedUser.UserName     = user.UserName;
                        savedUser.firstName    = user.firstName;
                        savedUser.lastName     = user.lastName;
                        savedUser.phoneNumber  = user.phoneNumber;
                        savedUser.address      = user.address;
                        savedUser.Userpassword = "******";
                        savedUser.createDate   = DateTime.Now;
                        //savedUser.lastSignInDate = user.lastSignInDate;

                        db.SaveChanges();
                        authentication.AddUserToRole(savedUser.UserName, "User");

                        WebImage image = null;
                        if (Request != null)
                        {
                            image = WebImage.GetImageFromRequest();
                        }

                        if (image != null)
                        {
                            string realFileName      = savedUser.memberId + image.FileName;
                            string baseDirectory     = AppDomain.CurrentDomain.BaseDirectory;
                            string imageFolder       = ConfigurationManager.AppSettings["UserImagePath"].ToString();
                            string fullPath          = baseDirectory + imageFolder;
                            string relativeImagePath = imageFolder + "\\" + realFileName;

                            if (!Directory.Exists(fullPath))
                            {
                                Directory.CreateDirectory(fullPath);
                            }
                            string imagePath = fullPath + "\\" + realFileName;
                            image.Save(imagePath);
                            savedUser.profilePicture = relativeImagePath;
                            db.SaveChanges();
                        }

                        TempData["Msg"] = string.Format("User '{0}' has been created.", savedUser.UserName);
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                    return(View());
                }
                System.Web.Routing.RouteValueDictionary value = new System.Web.Routing.RouteValueDictionary();
                value.Add("id", savedUser.memberId);
                return(RedirectToAction("Details", value));
            }

            return(View(user));
        }