public string Edit_article(string title, string content, int ArticleID, string picPath, int[] Activities) { TicketSysEntities db = new TicketSysEntities(); Article article = db.Article.Where(n => n.ArticleID == ArticleID).FirstOrDefault(); if (article != null) { article.ArticleTitle = title; article.ArticleContent = content; article.Date = DateTime.Now; if (!string.IsNullOrEmpty(picPath)) { article.Picture = picPath; } if (Activities != null) { //把先前的紀錄清空 foreach (var items in db.Ad_Article_Activity.Where(n => n.ArticleID == ArticleID)) { db.Ad_Article_Activity.Remove(items); } //加入新的活動連結 foreach (var item in Activities) { Ad_Article_Activity ad_Article_Activity = new Ad_Article_Activity(); ad_Article_Activity.ActivityID = item; ad_Article_Activity.ArticleID = ArticleID; db.Ad_Article_Activity.Add(ad_Article_Activity); } } db.SaveChanges(); } return("OK"); }
public string Add_article(string title, string content, string picPath, int[] Activities) { if (Session[CDictionary.SK_Logined_Member] == null) { return("Fail"); } try { if (string.IsNullOrEmpty(title)) { return("標題不得空白"); } if (string.IsNullOrEmpty(content)) { return("內文不得空白"); } //其他部分 TicketSysEntities db = new TicketSysEntities(); Member member = Session[CDictionary.SK_Logined_Member] as Member;//如果轉型失敗,回傳null; Article article = new Article(); article.MemberID = member.MemberID; article.Date = DateTime.Now; article.ArticleCategoryID = 1; article.ArticleTitle = title; article.ArticleContent = content; if (string.IsNullOrEmpty(picPath)) { article.Picture = picPath; } //article 的活動 if (Activities != null) { foreach (var item in Activities) { Ad_Article_Activity ad_Article_Activity = new Ad_Article_Activity(); ad_Article_Activity.ActivityID = item; ad_Article_Activity.ArticleID = article.ArticleID; db.Ad_Article_Activity.Add(ad_Article_Activity); } } db.Article.Add(article); db.SaveChanges(); return("OK"); } catch (Exception ex) { return(ex.Message); } }