//指定类型的文章个数 private void GetSortToArticleCount(HttpContext context) { tb_sorts sort = db.tb_sorts.SqlQuery("select * from tb_sorts where sort_name = '" + context.Request.Form["sort_name"] + "'").FirstOrDefault(); tb_set_article_sort[] sort_articles = db.tb_set_article_sort.SqlQuery("select * from tb_set_article_sort where sort_id =" + sort.sort_id).ToArray(); context.Response.Write(f.StToJSON(Convert.ToString(sort_articles.Length))); }
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 + "\"}"); }
//指定栏目文章个数 private void Getsortcount(HttpContext context) { tb_sorts sort = db.tb_sorts.SqlQuery("select * from tb_sorts where sort_id = '" + context.Request.Form["ID"] + "'").FirstOrDefault(); Count = db.tb_labels.SqlQuery("select * from tb_labels where label_alias = '" + sort.sort_name + "'").Count(); context.Response.Write(f.FromJSON <object>(Convert.ToString(Count))); }
public ActionResult DeleteConfirmed(long id) { tb_sorts tb_sorts = db.tb_sorts.Find(id); db.tb_sorts.Remove(tb_sorts); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: Category/Edit/5 public ActionResult Edit(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tb_sorts tb_sorts = db.tb_sorts.Find(id); if (tb_sorts == null) { return(HttpNotFound()); } return(View(tb_sorts)); }
//改变文章标签 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 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)); }