Exemple #1
0
        public ActionResult ZItem(int id = -1)
        {
            ZDev model;

            if (id < 0)
            {
                model = new ZDev();
            }
            else
            {
                model = _zDevService.GetById(id);
            }

            ZDevVm vm = Mapper.Map <ZDev, ZDevVm>(model);

            var options = new Dictionary <string, string>
            {
                { "saveZItemUrl", Url.Action("SaveZItem") },
                { "successUrl", Url.Action("Index") }
            };

            RequireJsOptions.Add("index", options);

            ViewBag.SuccessText = "Сохранено!";

            return(View(vm));
        }
Exemple #2
0
        public BsJsonResult SaveZItem(ZDevVm zItem)
        {
            const string imagesPath = "~/Content/Uploads/ZDev/";
            string       errorText  = null;
            ZDev         model      = null;

            var validImageTypes = new string[]
            {
                "image/gif",
                "image/jpeg",
                "image/pjpeg",
                "image/png"
            };

            if (zItem.ImageUpload != null && zItem.ImageUpload.ContentLength > 0 && !validImageTypes.Contains(zItem.ImageUpload.ContentType))
            {
                ModelState.AddModelError("ImageUpload", "Выберите GIF, JPG или PNG изображение.");
            }

            if (ModelState.IsValid)
            {
                var zDev = _zDevService.GetById(zItem.Id);
                model = Mapper.Map(zItem, zDev);
            }

            if (!ModelState.IsValid)
            {
                errorText = "Поля заполнены не верно. Черновик не был сохранен.";
            }

            if (errorText != null)
            {
                ModelState.AddFormError("Ошибка: ", errorText);

                return(new BsJsonResult(
                           new Dictionary <string, object> {
                    { "Errors", ModelState.GetErrors() }
                },
                           BsResponseStatus.ValidationError));
            }
            _zDevService.CreateOrUpdate(model);

            if (zItem.ImageUpload != null && zItem.ImageUpload.ContentLength > 0)
            {
                string folderPath = Server.MapPath(imagesPath);
                string imageName  = model.Id + Path.GetExtension(zItem.ImageUpload.FileName);
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                model.ImagePath = imagesPath + imageName;
                _zDevService.Update(model);
                zItem.ImageUpload.SaveAs(Path.Combine(folderPath, imageName));
            }

            return(new BsJsonResult(new { Status = BsResponseStatus.Success }));
        }