コード例 #1
0
        public async Task <ActionResult> UserCreate(CreateAccountModel model)
        {
            if (ModelState.IsValid)
            {
                //page.310
                AppUser user = new AppUser
                {
                    UserName        = model.Name,
                    Email           = model.Email,
                    PhoneNumber     = model.Phone,
                    CityID          = model.CityID,
                    CountryID       = model.CountryID,
                    ShippingAddress = model.ShippingAddress
                };
                IdentityResult result = await myUserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("UserIndex"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, model.CityID, model.CountryID);
            ViewBag.IsAdminAccess = true;
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View("~/Views/Home/CreateUser.cshtml", model));
        }
コード例 #2
0
        public async Task <ActionResult> CreateUser(CreateAccountModel model)
        {
            if (ModelState.IsValid)
            {
                //page.310
                AppUser user = new AppUser
                {
                    UserName        = model.Name,
                    Email           = model.Email,
                    PhoneNumber     = model.Phone,
                    CityID          = model.CityID,
                    CountryID       = model.CountryID,
                    ShippingAddress = model.ShippingAddress
                };
                IdentityResult result = await myUserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await UserManager.AddToRoleAsync(user.Id, "Users");  //新帳號預設Users權限
                    TempData["message"] = "創建帳號成功,請登入!";
                    return(RedirectToAction("Login", "Account", new { returnUrl = Url.Action("Index", "Home") }));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, model.CityID, model.CountryID);
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View(model));
        }
コード例 #3
0
 public ActionResult EditUser()
 {
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, CurrentUser.CityID, CurrentUser.CountryID);
     ViewBag.IsGoogleAccount = CheckIfGoogleAccount();
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     return(View(GetUserInfoEditModel(CurrentUser)));
 }
コード例 #4
0
        public JsonResult GetCountrySelectListItemJson(string cityID = "1")
        {
            IEnumerable <SelectListItem> ori = CityAndCountryPorvider.GetCountrySelectListItem(
                repository.Countries.Where(c => c.CityID == Convert.ToInt32(cityID)));
            var formattedData = ori.Select(p => new
            {
                Text  = p.Text,
                Value = p.Value
            }).ToArray();

            return(Json(formattedData, JsonRequestBehavior.AllowGet));
        }
コード例 #5
0
        public ActionResult UserCreate()
        {
            int defaultCityId = repository.Cities.OrderBy(c => c.ID).First().ID;

            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, defaultCityId);
            ViewBag.IsAdminAccess = true;
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View("~/Views/Home/CreateUser.cshtml"));
        }
コード例 #6
0
ファイル: CommonTest.cs プロジェクト: TysfHuang/PracticeWeb
        public void Can_Set_CityAndCountrySelectListItem_To_ViewBag_And_Check()
        {
            MyMock mock       = new MyMock();
            var    controller = new HomeController(mock.ProductRepository.Object, mock.UserManager.Object);

            CityAndCountryPorvider.SetSelectListToViewBag(controller, mock.ProductRepository.Object, 2);
            SelectListItem[] resultCity    = controller.ViewBag.CitySelectList as SelectListItem[];
            SelectListItem[] resultCountry = controller.ViewBag.CountrySelectList as SelectListItem[];

            Assert.AreEqual(4, resultCity.Length);
            Assert.AreEqual(2, resultCountry.Length);
            Assert.IsTrue(CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(controller));
        }
コード例 #7
0
ファイル: CommonTest.cs プロジェクト: TysfHuang/PracticeWeb
        public void Can_Get_CityAndCountryPorvider_CountrySelectListItem()
        {
            MyMock mock = new MyMock();

            SelectListItem[] result = CityAndCountryPorvider.GetCountrySelectListItem(mock.ProductRepository.Object.Countries, 2);

            Assert.AreEqual(4, result.Length);
            Assert.AreEqual("Country1", result[0].Text);
            Assert.AreEqual("1", result[0].Value);
            Assert.AreEqual("Country3", result[2].Text);
            Assert.AreEqual("3", result[2].Value);
            Assert.IsTrue(result[1].Selected);
        }
コード例 #8
0
 public ActionResult Checkout(Cart cart)
 {
     if (cart.Lines.Count() == 0)
     {
         TempData["message"] = "抱歉, 您的購物車是空的!";
         return(RedirectToAction("Index"));
     }
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, CurrentUser.CityID, CurrentUser.CountryID);
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     return(View(new ShippingDetails {
         CityID = CurrentUser.CityID,
         CountryID = CurrentUser.CountryID,
         ReceiverName = "",
         Line = CurrentUser.ShippingAddress
     }));
 }
コード例 #9
0
 public ActionResult CreateUser()
 {
     if (IsAuthenticated)
     {
         return(RedirectToAction("Index"));
     }
     try
     {
         int defaultCityId = repository.Cities.OrderBy(c => c.ID).FirstOrDefault().ID;
         CityAndCountryPorvider.SetSelectListToViewBag(this, repository, defaultCityId);
         if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         return(View(new CreateAccountModel()));
     }
     catch (NullReferenceException e)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
コード例 #10
0
 public ActionResult Checkout(Cart cart, ShippingDetails shippingDetails)
 {
     if (cart.Lines.Count() == 0)
     {
         ModelState.AddModelError("", "抱歉, 您的購物車是空的!");
     }
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, shippingDetails.CityID, shippingDetails.CountryID);
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     if (ModelState.IsValid)
     {
         //orderProcessor.ProcessOrder(cart, shippingDetails);
         orderLogger.RecordOrder(cart, CurrentUser.Id, shippingDetails.ReceiverName,
                                 repository.GetCityAndCountryName(shippingDetails.CountryID) + shippingDetails.Line);
         cart.Clear();
         return(View("Completed"));
     }
     else
     {
         return(View(shippingDetails));
     }
 }
コード例 #11
0
        public async Task <ActionResult> EditUser(UserInfoEditModel userInfo)
        {
            userInfo.Name  = CurrentUser.UserName;
            userInfo.Email = CurrentUser.Email;
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, userInfo.CityID, userInfo.CountryID);
            ViewBag.IsGoogleAccount = CheckIfGoogleAccount();
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (!ModelState.IsValid)
            {
                return(View(userInfo));
            }
            // 確認phone有符合所訂要求
            IdentityResult validPhone = null;

            if (userInfo.Phone != string.Empty)
            {
                CurrentUser.PhoneNumber = userInfo.Phone; //因ValidateAsync只接受User物件,因此這邊直接更改user的屬性
                validPhone = await myUserManager.UserValidator_ValidateAsync(CurrentUser);

                if (!validPhone.Succeeded)
                {
                    AddErrorsFromResult(validPhone);
                }
            }
            // 確認密碼
            IdentityResult validPass = null;

            if (userInfo.Password != string.Empty && userInfo.Password != null)
            {
                validPass = await myUserManager.PasswordValidator_ValidateAsync(userInfo.Password);

                if (validPass.Succeeded)
                {
                    CurrentUser.PasswordHash = myUserManager.PasswordHasher_HashPassword(userInfo.Password);
                }
                else
                {
                    AddErrorsFromResult(validPass);
                }
            }
            if ((validPhone.Succeeded && validPass == null) ||
                (validPhone.Succeeded && userInfo.Password != string.Empty && validPass.Succeeded))
            {
                CurrentUser.CityID          = userInfo.CityID;
                CurrentUser.CountryID       = userInfo.CountryID;
                CurrentUser.ShippingAddress = userInfo.ShippingAddress;
                IdentityResult result = await myUserManager.UpdateAsync(CurrentUser);

                if (result.Succeeded)
                {
                    TempData["message"] = "修改資料成功!";
                    return(RedirectToAction("EditUser"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return(View(userInfo));
        }