Esempio n. 1
0
        public ActionResult GetTemplatePartial()
        {
            IBaseModel viewModel;

            switch (Request.Form["template"].Last())
            {
            case "1": viewModel = new Template01ViewModel();
                break;

            case "2":
                viewModel = new Template02ViewModel();
                break;

            case "3":
                viewModel = new Template03ViewModel();
                break;

            case "4":
                viewModel = new Template04ViewModel();
                break;

            case "5":
                viewModel = new Template05ViewModel();
                break;

            case "6":
                viewModel = new Template06ViewModel();
                break;

            default: viewModel = new Template06ViewModel();
                break;
            }

            if (Request.Form.IsValid(viewModel.GetType()))
            {
                viewModel.Bind(Request.Form);
            }
            else
            {
                if (!Request.Form.ContainsKey("Get"))
                {
                    ModelState.AddModelError("Valid", "Ошибка валидации. Не все поля заполнены");
                }
                var wrapper = new StorageManager();
                ViewBag.Chapters = wrapper.GetChapterList();

                return(PartialView($"../Shared/Template{Request.Form["template"]}/_Form", viewModel.GetPartialModel(Request.Form)));
            }
            return(PartialView($"../Shared/Template{Request.Form["template"].Last()}/_Preview", viewModel));
        }
Esempio n. 2
0
        public IBaseModel Unzip(byte[] zipContent, string templateId)
        {
            IBaseModel model;

            switch (templateId)
            {
            case "1":
                model = new Template01ViewModel();
                break;

            case "2":
                model = new Template02ViewModel();
                break;

            case "3":
                model = new Template03ViewModel();
                break;

            case "4":
                model = new Template04ViewModel();
                break;

            case "5":
                model = new Template05ViewModel();
                break;

            case "6":
                model = new Template06ViewModel();
                break;

            default:
                model = new Template06ViewModel();
                break;
            }

            var stream = new MemoryStream();
            var type   = model.GetType();

            stream.Write(zipContent, 0, zipContent.Length);

            using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, true))
            {
                foreach (var entry in archive.Entries)
                {
                    using (var reader = new StreamReader(entry.Open()))
                    {
                        var name     = entry.FullName.Substring(0, entry.FullName.IndexOf('.'));
                        var property = type.GetProperty(name);

                        if (!name.Contains("ImgUrl"))
                        {
                            property.SetValue(model, reader.ReadToEnd());
                        }
                        else
                        {
                            using (var binaryReader = new BinaryReader(entry.Open()))
                            {
                                property.SetValue(model, binaryReader.ReadBytes((int)entry.Length));
                            }
                        }
                    }
                }
            }

            return(model);
        }