public IActionResult Create(NewsCategoryView newNewsCategory) { if (ModelState.IsValid) { _context.Add(newNewsCategory.NewsCategory); _context.SaveChanges(); // Add articles to the category if (newNewsCategory.SelectedNewsArticles != null) { foreach (int id in newNewsCategory.SelectedNewsArticles) { _context.NewsArticleCategory.Add(new NewsArticleCategory() { NewsCategoryID = newNewsCategory.NewsCategory.NewsCategoryID, NewsArticleID = id }); } } _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(newNewsCategory)); }
private NewsCategoryView getNewsCategoriesView(NewsCategory nc, SelectList courseList) { NewsCategoryView ncView = new NewsCategoryView() { NewsCategory = nc, NewsArticleList = courseList, SelectedNewsArticles = nc.NewsArticleCategories.Select(sc => sc.NewsArticleID) }; return(ncView); }
[ActionName("Edit")] //重名处理 public ActionResult EditCategory() { var category = new NewsCategory(); //使用白名单更新实体,防止表单绑定分配漏洞 var categoryWhite = new[] { "ParentID", "Name", "DisplayName", "Order", "Describe" }; var isValid = TryUpdateModel(category, categoryWhite); if (isValid) { _service.AddNewsCategory(category); return(RedirectToAction("List")); } else { var categoryView = new NewsCategoryView(); isValid = TryUpdateModel(categoryView, categoryWhite); return(View(categoryView));//返回带错误信息的模型 } }
public ActionResult Edit(int?id, int?parentId) { var categoryView = new NewsCategoryView(); if (id == null) { categoryView.ParentID = parentId; return(View(categoryView)); } else { var category = _service.GetCategoryById(id.Value); categoryView.ID = category.ID; categoryView.ParentID = category.ParentID; categoryView.Name = category.Name; categoryView.DisplayName = category.DisplayName; categoryView.Order = category.Order; categoryView.Describe = category.Describe; return(View(categoryView)); } }
public IActionResult Edit(int id, NewsCategoryView updatedNewsCategory) { if (id != updatedNewsCategory.NewsCategory.NewsCategoryID) { return(NotFound()); } if (ModelState.IsValid) { // First remove all the current couplings between this category and articles var rml = _context.NewsArticleCategory.Where(sc => sc.NewsCategoryID == id); _context.RemoveRange(rml); _context.SaveChanges(); // Add all the selected articles to the current category updatedNewsCategory.NewsCategory.NewsArticleCategories = new List <NewsArticleCategory>(); if (updatedNewsCategory.SelectedNewsArticles != null) { foreach (int courseID in updatedNewsCategory.SelectedNewsArticles) { _context.NewsArticleCategory.Add(new NewsArticleCategory() { NewsCategoryID = updatedNewsCategory.NewsCategory.NewsCategoryID, NewsArticleID = courseID }); } } _context.NewsCategory.Update(updatedNewsCategory.NewsCategory); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(updatedNewsCategory.NewsCategory)); }
public void CreateInNewsCategoriesController() { // Create a NewsCategoryView (for the create function) NewsCategoryView na = new NewsCategoryView() { NewsCategory = new NewsCategory() { Name = "Nieuws" } }; // Mocking var dbContext = Tools.MockTestDatabaseContext(); var controller = new NewsCategoriesController(dbContext); // Create a category var result = controller.Create(na); // Check if the result is a redirect Assert.IsType <RedirectToActionResult>(result); // Check if the name is saved correct Assert.Contains(na.NewsCategory.Name, dbContext.NewsCategory.FirstOrDefault().Name); }