public IActionResult newpost(PostViewModel pvm) { if (!admin) { return(View("error")); } if (pvm.categoryNames == null) { ViewBag.admin = admindisplay; ViewBag.user = userdisplay; ViewBag.categories = _categoryrepository.GetAll(); ViewBag.header = ViewBag.header = _headerrepository.GetById(17); return(View()); } _headerrepository.Create(pvm.postHeader); pvm.post.date = System.DateTime.Now; pvm.post.headerId = pvm.postHeader.id; pvm.post.viewCount = 0; _postrepository.Create(pvm.post); foreach (var item in pvm.categoryNames) { PostCategory pc = new PostCategory(); pc.post = pvm.post; pc.category = _categoryrepository.GetAll().Where(c => c.name == item).ToList()[0]; _postcategoryrepository.Create(pc); } return(RedirectToAction("index")); }