public ActionResult AddEdit(int? id) { Content content; if (id.HasValue) { using (var context = new ContentStorage()) { content = context.Content.Select(c => c).Where(c => c.Id == id).First(); } } else { content = new Content(); } return View(content); }
public ActionResult AddEdit(Content content, int? Id) { if (String.IsNullOrEmpty(content.Title)) ModelState.AddModelError("Title", "Title is required!"); if (String.IsNullOrEmpty(content.Text)) ModelState.AddModelError("Text", "Text is required!"); if (String.IsNullOrEmpty(content.ContentId)) ModelState.AddModelError("ContentId", "ContentId is required!"); content.Text = HttpUtility.HtmlDecode(content.Text); if (ModelState.IsValid) { using (var context = new ContentStorage()) { if (Id.HasValue && Id.Value > 0) { content.Id = Id.Value; object originalItem; EntityKey entityKey = new EntityKey("ContentStorage.Content", "Id", content.Id); if (context.TryGetObjectByKey(entityKey, out originalItem)) { context.ApplyPropertyChanges(entityKey.EntitySetName, content); } } else { context.AddToContent(content); } context.SaveChanges(); } return RedirectToAction("Index", "Content", new {area = "", id = content.ContentId}); } return View(content); }
public ActionResult Update(Content content) { using (var context = new ContentStorage()) { var contactsInfo = context.Content.Select(c => c).Where(c => c.IsContactsPage).First(); content.Id = contactsInfo.Id; content.Text = HttpUtility.HtmlDecode(content.Text); content.IsContactsPage = true; content.ContentId = "Contacts"; content.Title = "Контакты"; object originalItem; EntityKey entityKey = new EntityKey("ContentStorage.Content", "Id", content.Id); if (context.TryGetObjectByKey(entityKey, out originalItem)) { context.ApplyPropertyChanges(entityKey.EntitySetName, content); } context.SaveChanges(); } return RedirectToAction("Index", "Contacts", new { area = "" }); }