public ActionResult CreateNews() { var model = new EditNewsModel(); FillResourceEditNewsModel(model); return View("EditNews", model); }
private void ValidateUploadFile(EditNewsModel model) { if (model.ImageUploadFile != null && model.ImageUploadFile.FileName != null) { try { if (model.ImageUploadFile.ContentLength > 102400) ModelState.AddModelError(string.Empty, "Размер файла не выше 100кб"); if (!Constants.AcceptImage.Contains(model.ImageUploadFile.ContentType)) ModelState.AddModelError(string.Empty, "Требуемые форматы: *.jpg, *.jpeg, *.png, *.gif"); if (ModelIsValid) { var image = Image.FromStream(model.ImageUploadFile.InputStream); if (image.Width > 150 || image.Height > 150) ModelState.AddModelError(string.Empty, "Разрешение фото не должно превышать 150х150"); } if (ModelIsValid) { var fileName = string.Concat(StringGeneration.Generate(20), Path.GetExtension(model.ImageUploadFile.FileName)); var path = Path.Combine(Server.MapPath(Constants.NewsPreviewsPath), Path.GetFileName(fileName)); while (System.IO.File.Exists(path)) { fileName = string.Concat(StringGeneration.Generate(20), Path.GetExtension(model.ImageUploadFile.FileName)); } model.ImageUploadFile.SaveAs(path); if (!string.IsNullOrWhiteSpace(model.ImagePath) && System.IO.File.Exists(model.ImagePath)) System.IO.File.Delete(model.ImagePath); model.ImagePath = string.Concat(Constants.NewsPreviewsPath, fileName); } } catch (Exception) { ModelState.AddModelError(string.Empty, "Ошибка при сохранении файла"); } } }
public ActionResult EditNews(EditNewsModel model) { if (ModelIsValid) { model.PlatformIds.Clear(); if (!string.IsNullOrWhiteSpace(model.HiddenPlatformIds)) { var platformIds = model.HiddenPlatformIds.Split(','); foreach (var platformId in platformIds) { int id; if (int.TryParse(platformId, out id)) model.PlatformIds.Add(id); } } ValidateUploadFile(model); if (ModelIsValid) { var data = new NewsData { Id = model.Id.GetValueOrDefault(), Description = model.Description, Title = model.Title, NewsCategory = new NewsCategoryData { Id = model.NewsCategoryId }, WriterId = CurrentUser.Id, PlatformIds = model.PlatformIds, ImagePath = model.ImagePath, Annotation = model.Annotation, BlockComments = model.BlockComments }; Execute(() => _newsService.SaveNews(data)); if (ModelIsValid) { return data.Id == default(int) ? RedirectToAction<NewsController>(o => o.News()) : RedirectToAction<NewsController>(o => o.ViewNews(data.Id)); } } } FillResourceEditNewsModel(model); return View(model); }
private void FillResourceEditNewsModel(EditNewsModel model) { model.NewsCategories = Execute(() => _newsService.GetNewsGategories()) ?? new List<NewsCategoryData>(); model.Platforms = Execute(() => _resourceService.GetPlatforms()) ?? new List<PlatformData>(); }
public ActionResult EditNews(int? id) { if (id.HasValue) { var model = new EditNewsModel { Id = id.GetValueOrDefault() }; var news = Execute(() => _newsService.GetNews(model.Id.GetValueOrDefault())); if (news == null) return RedirectToAction<NewsController>(o => o.News()); if (CurrentUser.IsMainAdmin || CurrentUser.IsAdmin || (CurrentUser.IsModerator && CurrentUser.Id == news.WriterId)) { model.CopyFrom(news); FillResourceEditNewsModel(model); return View(model); } return RedirectToAction<NewsController>(o => o.CreateNews()); } return RedirectToAction<NewsController>(o => o.CreateNews()); }