public ActionResult New(int? categoryId) { using (UnitOfWorkManager.NewUnitOfWork()) { var allowedCategories = this._categoryService.GetAllowedCategories(UsersRole).ToList(); if (allowedCategories.FirstOrDefault(x => x.Id == categoryId) == null) { categoryId = 0; } if (allowedCategories.Any()) { var viewModel = new CreateTopicViewModel { Categories = allowedCategories, LoggedOnUser = LoggedOnUser, Category = (int)categoryId }; return View(viewModel); } } return View(); }
public ActionResult New(CreateTopicViewModel model) { if(ModelState.IsValid) { Category category; var topic = new Topic(); using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork()) { category = this._categoryService.Get(model.Category); var permissions = RoleService.GetPermissions(category, UsersRole); if (permissions[AppConstants.PermissionDenyAccess].IsTicked || permissions[AppConstants.PermissionReadOnly].IsTicked || permissions[AppConstants.PermissionCreateTopics].IsTicked) { ModelState.AddModelError(string.Empty, "No Permission"); } else { topic = new Topic { Name = model.Name, Category = category, User = LoggedOnUser }; if (!string.IsNullOrEmpty(model.Content)) { topic = this._topicService.Add(topic); unitOfWork.SaveChanges(); this._topicService.AddLastPost(topic, model.Content); // 添加标签 if (!string.IsNullOrEmpty(model.Tags)) { this._topicTagService.Add(model.Tags.ToLower(), topic); } try { unitOfWork.Commit(); return Redirect(topic.NiceUrl); } catch (Exception ex) { unitOfWork.Rollback(); LoggingService.Error(ex); ModelState.AddModelError(string.Empty, "添加主题失败"); } } else { unitOfWork.Rollback(); ModelState.AddModelError(string.Empty, "添加主题失败"); } } } } return RedirectToAction("New"); }