public ActionResult Create(Site site) { var image = WebImage.GetImageFromRequest("Logo"); if (ModelState.IsValid) { if (image != null) { if (image.Width > 256) { image.Resize(256, ((256 * image.Height) / image.Width)); } var newPath = "~/Images/Logos/site_" + site.Id + "_logo." + image.ImageFormat; image.Save(newPath); site.Logo = Url.Content(newPath); } else { site.Logo = null; } site.Last_edited = DateTime.Now; if ((from p in db.Sites where p.SiteName == site.SiteName select p).Count() == 0) { db.Sites.Add(site); db.SaveChanges(); return RedirectToAction("Index"); } else { // так и не разобрался как правильно передать текст ошибки, чтобы он отображался в Html.ValidationMessageFor ModelState.AddModelError("Site", "Такое имя уже существует"); } } ViewBag.Hosting = new SelectList(db.HostingPlans, "Id", "Hosting", site.Hosting); return View("CreateEdit", site); }
public ActionResult Edit(Site site) { var image = WebImage.GetImageFromRequest("Logo"); if (ModelState.IsValid) { if (image != null) { if (image.Width > 256) { image.Resize(256, ((256 * image.Height) / image.Width)); } var newPath = "~/Images/Logos/site_" + site.Id + "_logo." + image.ImageFormat; image.Save(newPath); site.Logo = Url.Content(newPath); } else { site.Logo = null; } site.Last_edited = DateTime.Now; db.Entry(site).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.Hosting = new SelectList(db.HostingPlans, "Id", "Hosting", site.Hosting); return View("CreateEdit", site); }