public ActionResult Index([Bind(Include = "sort_id,sort_name,sort_alias,sort_description,parent_sort_id")] tb_sorts tb_sorts) { if (ModelState.IsValid) { tb_sorts.sort_name = HttpContext.Request.Form["name"]; tb_sorts.sort_alias = HttpContext.Request.Form["alias"]; tb_sorts.parent_sort_id = Convert.ToInt32(HttpContext.Request.Form["fid"]); tb_sorts.sort_description = HttpContext.Request.Form["describe"]; db.tb_sorts.Add(tb_sorts); tb_labels Labels = new tb_labels(); string[] label_name = HttpContext.Request.Form["keywords"].Split(','); foreach (string item in label_name) { tb_labels new_labels = db.tb_labels.SqlQuery("select * from tb_labels where label_name = '" + item + "'").FirstOrDefault(); if (new_labels == null) { Labels.label_name = item; Labels.label_alias = tb_sorts.sort_name; Labels.label_description = ""; db.tb_labels.Add(Labels); } } db.SaveChanges(); return(View(db.tb_sorts.ToList())); } return(View()); }
//栏目,标签值 private void GetSortAndLabel(HttpContext context) { tb_set_article_sort set_sort = db.tb_set_article_sort.SqlQuery("select * from tb_set_article_sort where article_id =" + context.Request.Form["sort"]).FirstOrDefault(); tb_sorts sort = db.tb_sorts.Find(set_sort.sort_id); tb_set_article_label[] set_label = db.tb_set_article_label.SqlQuery("select * from tb_set_article_label where article_id = " + context.Request.Form["label"]).ToArray(); tb_labels label = new tb_labels(); string[] json = new string[set_label.Length]; for (int i = 0; i < set_label.Length; i++) { label = db.tb_labels.Find(Convert.ToInt32(set_label[i].label_id)); json[i] = f.OjToJson(label); json[i] = json[i].Replace("\"", "\\\""); json[i] = json[i].Replace(Convert.ToString('"'), "\\'"); json[i] = "\\\"json" + (i + 1) + "\\\":\\\"" + json[i] + "\\\""; } string labelStr = "{" + (string.Join(",", json)) + "}"; //string json1 = f.StToJSON(sort.sort_name); //string json2 = f.StToJSON(label.label_name); string sortStr = f.OjToJson(sort); sortStr = sortStr.Replace("\"", "\\\""); //context.Response.Write("{\"sort_name\":\"" + json1 + "\",\"label_name\":\"" + json2 + "\"}"); context.Response.Write("{\"sort\":\"" + sortStr + "\",\"label\":\"" + labelStr + "\"}"); }
//改变文章标签 public void ArticleLabel(tb_articles articles, tb_labels label, string item) { if (label != null) { tb_set_article_label article_label = db.tb_set_article_label.SqlQuery("select * from tb_set_article_label where label_id =" + label.label_id).FirstOrDefault(); //指定文章标签id article_label.label_id = label.label_id; //给定标签id article_label.article_id = articles.article_id; //给文章id db.tb_set_article_label.Attach(article_label); db.Entry(article_label).State = EntityState.Modified; db.SaveChanges(); } else { tb_labels add_label = new tb_labels(); add_label.label_name = item; tb_sorts sorts = db.tb_sorts.SqlQuery("select * from tb_sorts where sort_id = " + Convert.ToInt32(HttpContext.Request.Form["category"])).FirstOrDefault(); add_label.label_alias = sorts.sort_name; add_label.label_description = ""; db.tb_labels.Add(add_label); db.SaveChanges(); } }
//指定文章标签和分类 private void ArticleOfSortOrLabel(HttpContext context) { string sortname = "", labelname = ""; tb_set_article_sort article_sort = db.tb_set_article_sort.SqlQuery("select * from tb_set_article_sort where article_id = " + id).FirstOrDefault(); tb_set_article_label[] article_label = db.tb_set_article_label.SqlQuery("select * from tb_set_article_label where article_id = " + id).ToArray(); if (article_sort != null && article_label.Length > 0) { tb_sorts sort = db.tb_sorts.Find(article_sort.sort_id); sortname = sort.sort_name; for (int i = 0; i < article_label.Length; i++) { tb_labels label = db.tb_labels.Find(article_label[i].label_id); labelname += label.label_name; if (i != article_label.Length - 1) { labelname += ","; } } } context.Response.Write("{\"sortname\":\"" + sortname + "\",\"labelname\":\"" + labelname + "\"}"); }
public ActionResult Add([Bind(Include = "article_id,user_id,article_title,article_content,article_description,article_label_img,article_views,article_comment_count,article_date,article_like_count")] tb_articles tb_articles) { if (ModelState.IsValid) { tb_articles.user_id = 1; tb_articles.article_title = HttpContext.Request.Form["title"]; tb_articles.article_content = HttpContext.Request.Form["content"]; tb_articles.article_description = HttpContext.Request.Form["describe"]; //文章描述 tb_articles.article_label_img = HttpContext.Request.Form["titlepic"]; //文章标签图片地址 tb_articles.article_views = 0; //流量量 tb_articles.article_comment_count = 0; //评论总数 tb_articles.article_date = Convert.ToDateTime(DateTime.Now.ToString()); tb_articles.article_like_count = 0; //点赞数 //文章设置关键字 if (HttpContext.Request.Form["keywords"] != "") { tb_articles.article_keyword = HttpContext.Request.Form["keywords"]; } db.tb_articles.Add(tb_articles); db.SaveChanges(); tb_articles now_article = db.tb_articles.SqlQuery("select * from tb_articles order by article_id desc").FirstOrDefault(); //文章设置分类 tb_set_article_sort article_sort = new tb_set_article_sort(); article_sort.article_id = now_article.article_id; article_sort.sort_id = Convert.ToInt32(HttpContext.Request.Form["category"]); db.tb_set_article_sort.Add(article_sort); db.SaveChanges(); //文章设置标签表 string[] label; if (HttpContext.Request.Form["tags"] != "")/* || HttpContext.Request.Form["keywords"] != ""*/ { label = HttpContext.Request.Form["tags"].Split(','); List <string> strList = new List <string>(label); for (int i = 0; i < label.Length; i++) { if (label[i] == "") { strList.RemoveAt(i); } } foreach (string item in strList) { tb_labels labels = db.tb_labels.SqlQuery("select * from tb_labels where label_name = '" + item + "'").FirstOrDefault(); if (labels != null) { tb_set_article_label article_label = new tb_set_article_label(); article_label.article_id = now_article.article_id; //给定文章id article_label.label_id = labels.label_id; //给定标签id db.tb_set_article_label.Add(article_label); db.SaveChanges(); } } } return(Redirect("/YjLihouT/Articles/Article?page=1")); } return(View(tb_articles)); }
public ActionResult UpdateArticle([Bind(Include = "article_id,user_id,article_title,article_content,article_description,article_label_img,article_views,article_comment_count,article_date,article_like_count")] tb_articles tb_articles, long?id) { if (ModelState.IsValid) { //修改需要对主键赋值,注意:这里需要对所有字段赋值,没有赋值的字段会用NULL更新到数据库 tb_articles = db.tb_articles.SqlQuery("select * from tb_articles where article_id = '" + id + "'").FirstOrDefault(); tb_articles.article_id = Convert.ToInt32(id); tb_articles.article_title = HttpContext.Request.Form["title"]; tb_articles.article_content = HttpContext.Request.Form["content"]; tb_articles.article_date = Convert.ToDateTime(DateTime.Now.ToString()); tb_articles.article_description = HttpContext.Request.Form["describe"]; //文章描述 tb_articles.article_label_img = HttpContext.Request.Form["titlepic"]; //文章标签图片地址 tb_articles.article_keyword = HttpContext.Request.Form["keywords"]; //文章关键字 //将实体附加到对象管理器中 db.tb_articles.Attach(tb_articles); //获取到user的状态实体,可以修改其状态 var setEntry = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(tb_articles); setEntry.SetModifiedProperty("article_title"); setEntry.SetModifiedProperty("article_content"); setEntry.SetModifiedProperty("article_date"); if (tb_articles.article_label_img != "") { setEntry.SetModifiedProperty("article_label_img"); } if (tb_articles.article_description != "") { setEntry.SetModifiedProperty("article_description"); } if (tb_articles.article_keyword != "") { setEntry.SetModifiedProperty("article_keyword"); } //文章设置标签表 string[] labels; tb_set_article_label[] article_labels = db.tb_set_article_label.SqlQuery("select * from tb_set_article_label where article_id =" + tb_articles.article_id).ToArray(); if (HttpContext.Request.Form["tags"] != "")/* || HttpContext.Request.Form["keywords"] != ""*/ { labels = HttpContext.Request.Form["tags"].Split(','); List <string> strList = new List <string>(labels); for (int i = 0; i < labels.Length; i++) { if (labels[i] == "") { strList.RemoveAt(i); } } if (article_labels.Length == strList.Count()) { foreach (string item in strList) { tb_labels label = db.tb_labels.SqlQuery("select * from tb_labels where label_name = '" + item + "'").FirstOrDefault(); if (label != null) { ArticleLabel(tb_articles, label, item); db.SaveChanges(); } } } if (article_labels.Length > strList.Count()) { for (int i = 0; i < article_labels.Length; i++) { tb_labels label = db.tb_labels.Find(article_labels[i].label_id); for (int j = 0; j < strList.Count(); j++) { if (label.label_name != strList[j]) { db.tb_set_article_label.Remove(article_labels[i]); db.SaveChanges(); } } } //for (int i = 0; i < labels.Length; i++) //{ // tb_labels label = db.tb_labels.SqlQuery("select * from tb_labels where label_name = '" + labels[i] + "'").FirstOrDefault(); // ArticleLabel(tb_articles, label, labels[i]); // db.SaveChanges(); //} } if (article_labels.Length < strList.Count()) { for (int i = article_labels.Length; i < strList.Count(); i++) { for (int j = 0; j < strList.Count(); j++) { for (int k = 0; k < article_labels.Length; k++) { tb_labels label2 = db.tb_labels.SqlQuery("select * from tb_labels where label_id = '" + article_labels[k].label_id + "'").FirstOrDefault(); if (label2.label_name != strList[j]) { tb_labels label = db.tb_labels.SqlQuery("select * from tb_labels where label_name = '" + strList[j] + "'").FirstOrDefault(); if (label != null) { tb_set_article_label article_Label = new tb_set_article_label(); article_Label.article_id = tb_articles.article_id; article_Label.label_id = label.label_id; db.tb_set_article_label.Add(article_Label); db.SaveChanges(); } } } } ; } } } ; return(Redirect("/YjLihouT/Articles/Article?page=1")); } return(View(tb_articles)); }
public ActionResult Edit([Bind(Include = "sort_id,sort_name,sort_alias,sort_description,parent_sort_id")] tb_sorts tb_sorts, long?id) { if (ModelState.IsValid) { //修改需要对主键赋值,注意:这里需要对所有字段赋值,没有赋值的字段会用NULL更新到数据库 tb_sorts = db.tb_sorts.SqlQuery("select * from tb_sorts where sort_id = '" + id + "'").FirstOrDefault(); tb_sorts.sort_id = Convert.ToInt32(id); tb_sorts.sort_name = HttpContext.Request.Form["name"]; tb_sorts.sort_alias = HttpContext.Request.Form["alias"]; tb_sorts.sort_description = HttpContext.Request.Form["describe"]; tb_sorts.parent_sort_id = Convert.ToInt32(HttpContext.Request.Form["fid"]); //将实体附加到对象管理器中 db.tb_sorts.Attach(tb_sorts); //获取到user的状态实体,可以修改其状态 var setEntry = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(tb_sorts); setEntry.SetModifiedProperty("sort_name"); setEntry.SetModifiedProperty("sort_alias"); if (tb_sorts.sort_description != null) { setEntry.SetModifiedProperty("sort_description"); } if (tb_sorts.parent_sort_id != 0) { setEntry.SetModifiedProperty("parent_sort_id"); } tb_labels[] labels = db.tb_labels.SqlQuery("select * from tb_labels where label_alias = '" + tb_sorts.sort_name + "'").ToArray(); if (labels != null) { string[] str = HttpContext.Request.Form["keywords"].Split(','); List <string> strList = new List <string>(str); for (int i = 0; i < str.Length; i++) { if (str[i] == "") { strList.RemoveAt(i); } } if (labels.Length == strList.Count()) { for (int i = 0; i < labels.Length; i++) { labels[i].label_name = str[i]; //将实体附加到对象管理器中 db.tb_labels.Attach(labels[i]); // 把当前实体的状态改为Modified db.Entry(labels[i]).State = EntityState.Modified; } } if (labels.Length < strList.Count()) { for (int i = 0; i < labels.Length; i++) { labels[i].label_name = str[i]; //将实体附加到对象管理器中 db.tb_labels.Attach(labels[i]); // 把当前实体的状态改为Modified db.Entry(labels[i]).State = EntityState.Modified; } for (int i = labels.Length; i < strList.Count(); i++) { tb_labels label = new tb_labels(); label.label_name = str[i]; label.label_description = ""; label.label_alias = tb_sorts.sort_name; db.tb_labels.Add(label); db.SaveChanges(); } } if (labels.Length > strList.Count()) { for (int i = 0; i < strList.Count(); i++) { labels[i].label_name = str[i]; //将实体附加到对象管理器中 db.tb_labels.Attach(labels[i]); // 把当前实体的状态改为Modified db.Entry(labels[i]).State = EntityState.Modified; } for (int i = strList.Count(); i < labels.Length; i++) { for (int k = 0; k < labels.Length; k++) { bool isremove = true; for (int j = 0; j < str.Length; j++) { if (str[k] == labels[j].label_name) { isremove = false; } } if (isremove) { db.tb_labels.Remove(labels[i]); db.SaveChanges(); } } } } } db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tb_sorts)); }