コード例 #1
0
        public ActionResult AddBook(HttpPostedFileBase file, AddProductViewModel model)
        {
            Session["Username"] = "******";
            var userName = Session["Username"];
            var imagePath = CreateMD5(model.Title + DateTime.Now + DateTime.Now.Millisecond + model.Detail) ;

            var addViewModel = new ProductModel();
            addViewModel.Title = model.Title;
            addViewModel.Author = model.Author;
            addViewModel.Image = imagePath;
            addViewModel.Detail = model.Detail;
            addViewModel.Balance = model.Balance;
            addViewModel.Isbn = model.Isbn;
            addViewModel.Price = model.Price;
            addViewModel.CategoryId = model.CategoryId;
            addViewModel.CreateBy = userName.ToString();
            addViewModel.CreateTime = DateTime.Now;
            addViewModel.UpdateBy = "Test Admin";
            addViewModel.UpdateTime = DateTime.Now;
            addViewModel.IsActive = true;

            bool uploadImageIsComplete = true;
            if (file != null && file.ContentLength > 0)
                try
                {
                    var fileName = imagePath + ".jpg";
                    var path = Path.Combine(Server.MapPath("~/Content/images/Product/"), fileName);
                    file.SaveAs(path);
                }
                catch (Exception ex)
                {
                    uploadImageIsComplete = false;
                }
            else
            {
                uploadImageIsComplete = false;
            }

            if (uploadImageIsComplete)
            {
                ProductService productService = new ProductService();
                if (productService.Save(addViewModel))
                {
                    // return RedirectToRoute("~Success");
                }
                else
                {
                   // return RedirectToRoute("~Error");
                }
            }

            return RedirectToAction("ManageProductView", "ManageProduct");
        }
コード例 #2
0
 public ActionResult AddBook()
 {
     var categoryService = new CategoryService();
     var categories = categoryService.GetCategory();
     var model = new AddProductViewModel();
     model.Categories = new List<SelectListItem>();
     foreach (var item in categories)
     {
         model.Categories.Add(new SelectListItem() { Value = item.Id.ToString(), Text = item.name });
     }
     return View(model);
 }