示例#1
0
        public ActionResult ShowInformalPost()
        {
            var ctx = new ApplicationDbContext();
            var pvm = new InformalPostViewModel();

            pvm.InformalPosts = ctx.InformalPost.OrderByDescending(p => p.Date).ToList();

            return(View(pvm));
        }
        public ActionResult InformalPosts(int?category)
        {
            var model = new InformalPostViewModel();

            if (category.HasValue)
            {
                model.InformalCategories = new SelectList(db.InformalCategories, "Id", "Name");
                model.InformalPosts      = db.InformalPosts.Include("AuthorId")
                                           .Where(p => p.InformalCategories.Any(c => c.Id == category))
                                           .OrderByDescending(x => x.PostTime)
                                           .ToList();
                model.SelectedCategoryId = Convert.ToInt32(category);
            }
            else
            {
                model.InformalPosts      = db.InformalPosts.Include("InformalCategories").Include("AuthorId").OrderByDescending(x => x.PostTime).ToList();
                model.InformalCategories = new SelectList(db.InformalCategories, "Id", "Name");
            }


            return(View(model));
        }
示例#3
0
        public ActionResult WriteInformalPost(InformalPostViewModel postmodel)
        {
            var             ctx          = new ApplicationDbContext();
            var             userId       = User.Identity.GetUserId();
            var             value        = Request["Category"];
            var             store        = new UserStore <ApplicationUser>(new ApplicationDbContext());
            var             userManager  = new UserManager <ApplicationUser>(store);
            ApplicationUser user         = userManager.FindById(userId);
            var             informalpost = new InformalPostModel
            {
                Title      = postmodel.Title,
                Content    = postmodel.Content,
                Author     = User.Identity.GetUserName(),
                Date       = DateTime.Now,
                CategoryId = int.Parse(value)
            };

            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);
                    informalpost.FilePath = "~/UploadedFiles/" + fileName;
                    postmodel.File.SaveAs(path);
                }
                ctx.InformalPost.Add(informalpost);
                ctx.SaveChanges();
            }
            else
            {
                postmodel.InformalPosts = ctx.InformalPost.OrderByDescending(p => p.Date).ToList();
                return(View("ShowInformalPost", postmodel));
            }

            return(RedirectToAction("ShowInformalPost"));
        }