Пример #1
0
        //查看文章
        public ActionResult Details(int Id)
        {
            var article = db.tArticles.FirstOrDefault(m => m.Id == Id);

            if (article == null)
            {
                return(View("error"));
            }

            article.ReadCount++;
            db.SaveChanges();
            return(View(article));
        }
Пример #2
0
        public ActionResult PostArticle(tArticle article)
        {
            if (User.Identity.IsAuthenticated)
            {
                var user = db.tAccounts.FirstOrDefault(m => m.UserName == User.Identity.Name);
                if (user != null)
                {
                    //存文章
                    article.Author  = user.UserName;
                    article.AddTime = DateTime.Now;

                    //添加标签
                    string tags = Request.Params["tags"];
                    if (!string.IsNullOrEmpty(tags))
                    {
                        string[] ts = tags.Split(',');
                        foreach (var s in ts)
                        {
                            if (!string.IsNullOrEmpty(s))
                            {
                                var tag = db.tTags.FirstOrDefault(m => m.TagName == s);
                                if (tag == null)
                                {
                                    article.tTags.Add(new tTag()
                                    {
                                        TagName = s
                                    });
                                }
                                else
                                {
                                    article.tTags.Add(tag);
                                }
                            }
                        }
                    }

                    //关联分类
                    string cateName = Request.Params["Category"];
                    if (!string.IsNullOrEmpty(cateName))
                    {
                        var cate = db.tCategories.FirstOrDefault(m => m.CategoryName == cateName);
                        if (cate != null)
                        {
                            article.tCategories.Add(cate);
                        }
                    }

                    //关联到用户
                    user.tArticles.Add(article);

                    db.SaveChanges();
                }
            }
            return(View(article));
        }
Пример #3
0
        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="categoryName"></param>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public ActionResult AddCategory(string categoryName, int parentid)
        {
            var parentCategory = db.tCategories.FirstOrDefault(m => m.ParentId == parentid);

            if (parentCategory != null)
            {
                tCategory category = new tCategory()
                {
                    CategoryName = categoryName, ParentId = parentid, CategoryPath = parentCategory.CategoryPath + parentid
                };
                db.tCategories.AddObject(category);
                db.SaveChanges();
            }
            return(RedirectToAction("Category"));
        }