// GET: /PostInfo/Create
 public ActionResult Create(int cId)
 {
     ViewBag.CategoryId = new SelectList(db.Category.Where(c => c.CommunityId == cId), "Id", "Name");
     // ViewBag.Author = new SelectList(db.UserInfo, "id", "Name");
     ViewBag.cId = cId;
     var postInfo = new PostInfo();
     postInfo.Category = new List<Category>();
     PopulateAssignedPostCategory(postInfo);
     return View();
 }
        public ActionResult Create(PostInfo postinfo, string[] selectedCategory)
        {
            if (selectedCategory != null)
            {
                postinfo.Category = new List<Category>();
                foreach (var category in selectedCategory)
                {
                    var categoryToAdd = db.Category.Find(int.Parse(category));
                    postinfo.Category.Add(categoryToAdd);
                }
            }
            if (ModelState.IsValid)
            {
                string cIds = Request.Form["CategoryId"];
                int userId = ((UserInfo)Session["User"]).id;

                postinfo.CategoryIds = cIds;
                postinfo.Author = userId;
                //图片
                var file = Request.Files[0];
                var img = MyHelper.fileSaveAs(file, 0, 2);
                if (img.IndexOf("UpFile") > -1)
                {
                    postinfo.ImageURL = img;
                }
                else
                {
                    postinfo.ImageURL = "/Content/Images/images.jpg";
                }
                db.PostInfo.Add(postinfo);
                db.SaveChanges();
                return RedirectToAction("Index", new { cId = postinfo.CommunityId });
            }

            ViewBag.CategoryId = new SelectList(db.Category, "Id", "Name");
            ViewBag.Author = new SelectList(db.UserInfo, "id", "Name", postinfo.Author);
            PopulateAssignedPostCategory(postinfo);
            return View(postinfo);
        }
 private void PopulateAssignedPostCategory(PostInfo Post)
 {
     var allcategory = db.Category;
     var postCategory = new HashSet<int>(Post.Category.Select(c => c.Id));
     var viewModel = new List<AssignedPostCategory>();
     foreach (var category in allcategory)
     {
         viewModel.Add(new AssignedPostCategory
         {
             CategoryID = category.Id,
             CategoryName = category.Name,
             Assigned = postCategory.Contains(category.Id)
         });
     }
     ViewBag.Category = viewModel;
 }
        private void UpdatePostCategory(string[] selectedCategory, PostInfo postToUpdate)
        {
            if (selectedCategory == null)
            {
                postToUpdate.Category = new List<Category>();
                return;
            }

            var selectedCategoryHS = new HashSet<string>(selectedCategory);
            var postCategory = new HashSet<int>
                (postToUpdate.Category.Select(c => c.Id));
            foreach (var category in db.Category)
            {
                if (selectedCategoryHS.Contains(category.Id.ToString()))
                {
                    if (!postCategory.Contains(category.Id))
                    {
                        postToUpdate.Category.Add(category);
                    }
                }
                else
                {
                    if (postCategory.Contains(category.Id))
                    {
                        postToUpdate.Category.Remove(category);
                    }
                }
            }
        }
        public ActionResult Edit(PostInfo postinfo)
        {
            if (ModelState.IsValid)
            {
                //图片
                var file = Request.Files[0];
                var img = MyHelper.fileSaveAs(file, 0, 2);
                if (img.IndexOf("UpFile") > -1)
                {
                    postinfo.ImageURL = img;
                }
                string cIds = Request.Form["CategoryId"];
                postinfo.CategoryIds = cIds;

                db.Entry(postinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { cId = postinfo.CommunityId });

            }
            ViewBag.CategoryId = new SelectList(db.Category, "Id", "Name");
            ViewBag.Author = new SelectList(db.UserInfo, "id", "Name", postinfo.Author);
            return View(postinfo);
        }