コード例 #1
0
        public ActionResult WritePost(FormalPostViewModel postmodel)
        {
            var             ctx         = new ApplicationDbContext();
            var             userId      = User.Identity.GetUserId();
            var             store       = new UserStore <ApplicationUser>(new ApplicationDbContext());
            var             userManager = new UserManager <ApplicationUser>(store);
            ApplicationUser user        = userManager.FindById(userId);
            var             post        = new FormalPostModel
            {
                Title   = postmodel.Title,
                Content = postmodel.Content,
                Author  = User.Identity.GetUserName(),
                Date    = DateTime.Now
            };

            if (ModelState.IsValid)
            {
                if (postmodel.File != null && postmodel.File.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(postmodel.File.FileName);
                    var path     = Path.Combine(Server.MapPath("~/UploadedFiles/"), fileName);
                    post.FilePath = "~/UploadedFiles/" + fileName;
                    postmodel.File.SaveAs(path);
                }
                ctx.Post.Add(post);
                ctx.SaveChanges();
            }
            else
            {
                postmodel.Posts = ctx.Post.OrderByDescending(p => p.Date).ToList();
                return(View("ShowPost", postmodel));
            }

            return(RedirectToAction("ShowPost"));
        }
コード例 #2
0
        // GET: Posts
        public ActionResult FormalPosts(int?type, int?category)
        {
            var model = new FormalPostViewModel();

            if (type.HasValue)
            {
                var typeInt32 = Convert.ToInt32(type);
                model.SelectedTypeId   = typeInt32;
                model.FormalTypes      = new SelectList(db.FormalTypes, "Id", "Name");
                model.FormalCategories = new SelectList(db.FormalCategories.Where(c => c.Type.Id == typeInt32), "Id", "Name");
                model.FormalPosts      = db.FormalPosts.Include("AuthorId").Include("FormalCategories.Type")
                                         .Where(p => p.FormalCategories.Any(c => c.Type.Id == type))
                                         .OrderByDescending(x => x.PostTime)
                                         .ToList();
                if (category.HasValue)
                {
                    model.SelectedCategoryId = Convert.ToInt32(category);
                    model.FormalPosts        = db.FormalPosts.Include("AuthorId").Include("FormalCategories.Type")
                                               .Where(p => p.FormalCategories.Any(c => c.Id == category && c.Type.Id == type))
                                               .OrderByDescending(x => x.PostTime)
                                               .ToList();
                }
            }
            else
            {
                model.FormalTypes      = new SelectList(db.FormalTypes, "Id", "Name");
                model.FormalCategories = new SelectList(db.FormalCategories, "Id", "Name");
                model.FormalPosts      = db.FormalPosts.Include("FormalCategories").Include("AuthorId").OrderByDescending(x => x.PostTime).ToList();
            }



            return(View(model));
        }
コード例 #3
0
        public ActionResult ShowPost()
        {
            var ctx = new ApplicationDbContext();
            var pvm = new FormalPostViewModel();

            pvm.Posts = ctx.Post.OrderByDescending(p => p.Date).ToList();

            return(View(pvm));
        }
コード例 #4
0
        public ActionResult ShowFilteredPost()
        {
            var ctx   = new ApplicationDbContext();
            var pvm   = new FormalPostViewModel();
            var cat   = new CategoryModel();
            var value = int.Parse(Request["CategoryFilter"]);


            pvm.Posts = ctx.Post.Where(p => p.CategoryId == value).OrderByDescending(p => p.Date).ToList();
            return(View("ShowPost", pvm));
        }