public static void InsertOnSubmit(this DbSet<Article> source, Article article) { if (article.ArticleID == default(int)) { // New entity source.Add(article); } else { // Existing entity source.Attach(article); } }
public ActionResult CreateArticle(int? categoryId, string title, string summary, string body, string country, string state, string city, DateTime? releaseDate, DateTime? expireDate, bool? approved, bool? listed, bool? commentsEnabled, bool? onlyForMembers) { TheBeerHouseDataContext dc = new TheBeerHouseDataContext(); var categories = dc.Categories.GetCategories(); if (categoryId.HasValue && !String.IsNullOrEmpty(title) && !String.IsNullOrEmpty(body)) { try { Article article = new Article { CategoryID = categoryId.Value, Title = title, Path = title.ToUrlFormat(), Abstract = summary, Body = body, Country = country, State = state, City = city, ReleaseDate = releaseDate ?? DateTime.Today, ExpireDate = expireDate, Approved = approved ?? false, Listed = listed ?? false, CommentsEnabled = commentsEnabled ?? false, OnlyForMembers = onlyForMembers ?? false, AddedBy = User.Identity.Name, AddedDate = DateTime.Now }; dc.Articles.InsertOnSubmit(article); dc.SubmitChanges(); TempData["SuccessMessage"] = "Your article has been posted."; return RedirectToAction("ViewArticle", new { id = article.ArticleID, path = article.Path }); } catch (Exception exc) { TempData["ErrorMessage"] = exc.Message; } } ViewData["categoryId"] = new SelectList(categories, "CategoryID", "Title", categoryId); ViewData["title"] = title; ViewData["summary"] = summary; ViewData["body"] = body; ViewData["country"] = new SelectList(Iso3166CountryCodes.CountryDictonary, "Key", "Value", country ?? "US"); ViewData["state"] = state; ViewData["city"] = city; ViewData["releaseDate"] = releaseDate; ViewData["expireDate"] = expireDate; ViewData["approved"] = approved; ViewData["listed"] = listed; ViewData["commentsEnabled"] = commentsEnabled; ViewData["onlyForMembers"] = onlyForMembers; ViewData["PageTitle"] = "Create Article"; return View("CreateArticle"); }
public static void DeleteOnSubmit(this DbSet<Article> source, Article article) { source.Remove(article); }