public ActionResult ProductCreate() { Product product = new Product(); ViewBag.IsEditProcess = false; BrandAndCategoryProvider.SetSelectListToViewBag(this, repository); if (!BrandAndCategoryProvider.CheckIfSelectListOfViewBagCorrect(this)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } return(View("ProductEdit", product)); }
public void Can_Set_BrandAndCategorySelectListItem_To_ViewBag_And_Check() { MyMock mock = new MyMock(); var controller = new HomeController(mock.ProductRepository.Object, mock.UserManager.Object); BrandAndCategoryProvider.SetSelectListToViewBag(controller, mock.ProductRepository.Object); SelectListItem[] resultCategory = controller.ViewBag.CategoryList as SelectListItem[]; SelectListItem[] resultBrand = controller.ViewBag.BrandList as SelectListItem[]; Assert.AreEqual(4, resultCategory.Length); Assert.AreEqual(4, resultBrand.Length); Assert.IsTrue(BrandAndCategoryProvider.CheckIfSelectListOfViewBagCorrect(controller)); }
public void Can_Get_BrandAndCategoryPorvider_BrandSelectListItem() { MyMock mock = new MyMock(); SelectListItem[] result = BrandAndCategoryProvider.GetBrandListItem(mock.ProductRepository.Object.Brands, 2); Assert.AreEqual(4, result.Length); Assert.AreEqual("Brand1", result[0].Text); Assert.AreEqual("1", result[0].Value); Assert.AreEqual("Brand3", result[2].Text); Assert.AreEqual("3", result[2].Value); Assert.IsTrue(result[1].Selected); }
public async Task <ActionResult> ProductEdit(Product product, HttpPostedFileBase fileToUpload = null) { try { if (product == null || (product.ID != 0 && !repository.Products.Select(p => p.ID).Contains(product.ID))) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (product.CoverImagePath == "default") { product.CoverImagePath = GetUrlOfDefaultImageInBlob(); } if (ModelState.IsValid) { if (fileToUpload != null) { string fileUrl = await UploadImageAsync(fileToUpload); product.CoverImagePath = fileUrl; } if (product.ID == 0) { TempData["message"] = string.Format("{0} 已新增", product.Name); } else { TempData["message"] = string.Format("{0} 的修改已儲存", product.Name); } repository.SaveProduct(product); return(RedirectToAction("ProductIndex")); } } catch (DataException /* dex */) { ModelState.AddModelError("", "無法儲存修改值!"); } ViewBag.IsEditProcess = true; BrandAndCategoryProvider.SetSelectListToViewBag(this, repository, product.CategoryID, product.BrandID); if (!BrandAndCategoryProvider.CheckIfSelectListOfViewBagCorrect(this)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } return(View(product)); }
public ActionResult ProductEdit(int ID) { Product product = repository.Products .FirstOrDefault(p => p.ID == ID); if (product == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } ViewBag.IsEditProcess = true; BrandAndCategoryProvider.SetSelectListToViewBag(this, repository, product.CategoryID, product.BrandID); if (!BrandAndCategoryProvider.CheckIfSelectListOfViewBagCorrect(this)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } return(View(product)); }