Exemplo n.º 1
0
        public async Task <ActionResult> Create(AddClassTypeViewModel classType)
        {
            if (ModelState.IsValid)
            {
                // Verify that the user selected a file
                if (classType.ImageFile != null && classType.ImageFile.ContentLength > 0)
                {
                    if (classType.ImageFile.ContentLength > 20000000)
                    {
                        ModelState.AddModelError("", "Please ensure image file is less than 2MB");
                        return(RedirectToAction("Create"));
                    }

                    // extract only the filename
                    var fileName = Path.GetFileName(classType.ImageFile.FileName);

                    if (!fileName.Contains(".jpg") && !fileName.Contains(".png"))
                    {
                        ModelState.AddModelError("", "Please ensure file is in JPG or PNG format");
                        return(RedirectToAction("Create"));
                    }

                    if (fileName.Contains(" "))
                    {
                        ModelState.AddModelError("", "Image filename cannot contain spaces or special characters");
                        return(RedirectToAction("Create"));
                    }

                    var filePath = ConfigurationManager.AppSettings["ClassTypeAssetPath"];

                    // store the file inside ~/App_Data/classtype folder
                    var path = Path.Combine(Server.MapPath(filePath), fileName);
                    classType.ImageFile.SaveAs(path);

                    classType.ImageFileName = fileName;
                }

                // have not populated view yet - need to do that and then test

                var isSuccess = await _classTypeService.AddClassType(classType);

                if (isSuccess)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(View(classType));
        }
        public ActionResult Create(ViewModels.ClassType model)
        {
            if (ModelState.IsValid)
            {
                var dbModel = new Domain.ClassType();

                dbModel.InjectFrom(model);
                //classRepository.Add(dbModel);
                classServices.AddClassType(dbModel);
                //transform the object
                //unitofWork.Commit();
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }