/// <summary> /// Initializes a new instance of the <see cref="T:System.Object"/> class. /// </summary> public BannerJsonModel(Banner banner) { Id = banner.Id; Title = banner.Title; Img = banner.Img; Href = banner.Href; Sort = banner.Sort; Objects = banner.Objects; Extra = banner.Extra; Categories = banner.Categories; }
public JsonResult Save([ModelBinder(typeof(JsonModelBinder))] BannerJsonModel model) { try { // Репозиторий var repository = Locator.GetService<IBannersRepository>(); // Сохраняем файл с картинкой bool fileSubmitted = false; string submittedFileName = ""; var file = Request.Files["file"]; if (file != null && file.ContentLength > 0 && file.ContentType.Contains("image")) { fileSubmitted = true; var fileName = String.Format("{0}-{1}{2}", Path.GetFileNameWithoutExtension(file.FileName), new Random(System.Environment.TickCount).Next(65535), Path.GetExtension(file.FileName)); FileUtils.SavePostedFile(file, "banner", fileName); submittedFileName = fileName; } if (model.Id <= 0) { // Создаем новую страничку var banner = new Banner() { Title = model.Title, Img = fileSubmitted ? submittedFileName : model.Img, Href = model.Href, Sort = model.Sort, Html = model.Html, Objects = model.Objects, Categories = model.Categories, Extra = model.Extra, }; repository.Add(banner); // Сохраняем изменения repository.SubmitChanges(); } else { // Обновляем существующую var banner = repository.Load(model.Id); if (banner == null) { throw new ObjectNotFoundException(String.Format("Баннер с идентификатором {0} не найден", model.Id)); } banner.Title = model.Title; banner.Img = fileSubmitted ? submittedFileName : model.Img; banner.Href = model.Href; banner.Sort = model.Sort; banner.Html = model.Html; banner.Objects = model.Objects; banner.Categories = model.Categories; banner.Extra = model.Extra; // Сохраняем изменения repository.SubmitChanges(); } return JsonSuccess(); } catch (Exception e) { return JsonErrors(e); } }