示例#1
0
        private void SaveImage(CategoryModel viewModel, HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength <= 0)
                return;

            var fileName = Path.GetFileName(file.FileName);
            var extension = Path.GetExtension(fileName).ToLower();
            var validImageExtensions = new List<string>() { ".jpg", ".png", ".gif", ".bmp" };

            if (!validImageExtensions.Contains(extension))
                throw new ApplicationException("Invalid Image extension.");

            var savePath = string.Format("~/Images/{0}/", viewModel.Name);
            savePath = Server.MapPath(savePath);

            Directory.CreateDirectory(savePath);

            savePath = Path.Combine(savePath, fileName);

            file.SaveAs(savePath);

            viewModel.Image = fileName;
        }
示例#2
0
        public virtual ActionResult Edit(CategoryModel viewModel, HttpPostedFileBase file)
        {
            ValidateIsAdmin();

            SaveImage(viewModel, file);

            using (var context = new TTTEntities())
            {
                var record = context.refcategories.Single(a => a.ID == viewModel.ID);

                record.Name = viewModel.Name;
                record.Active = viewModel.Active;
                record.Type = viewModel.Type;

                if (!string.IsNullOrEmpty(viewModel.Image))
                    record.Image = viewModel.Image;

                context.SaveChanges();
            }

            return RedirectToAction(MVC.Admin.Category.Index());
        }
示例#3
0
        public virtual ActionResult Create(CategoryModel viewModel, HttpPostedFileBase file)
        {
            ValidateIsAdmin();

            SaveImage(viewModel, file);

            using (var context = new TTTEntities())
            {
                var newRecord = new refcategory
                {
                    Active = viewModel.Active,
                    Name = viewModel.Name,
                    CreateDT = DateTime.Now,
                    Type = viewModel.Type,
                    Image = viewModel.Image
                };

                context.refcategories.Add(newRecord);
                context.SaveChanges();
            }

            return RedirectToAction(MVC.Admin.Category.Index());
        }
示例#4
0
        public virtual ActionResult Edit(int id)
        {
            ValidateIsAdmin();

            using (var context = new TTTEntities())
            {
                var record = context.refcategories.Single(a => a.ID == id);
                var imagePath = string.Format("~/Images/{0}/", record.Name);
                var imageUrl =
                    string.IsNullOrEmpty(record.Image) ? "" : Path.Combine(imagePath, record.Image);

                var viewModel = new CategoryModel
                {
                    Active = record.Active,
                    ID = record.ID,
                    Name = record.Name,
                    Type = record.Type,
                    Image = imageUrl
                };

                return View(viewModel);
            }

        }
示例#5
0
        public virtual ActionResult Create()
        {
            ValidateIsAdmin();

            var viewModel = new CategoryModel { Active = true };

            return View(viewModel);
        }