public ActionResult Create(CatModel model) { if (this.ModelState.IsValid) { string uniqueCheck = CategoryComp.GetCategoryID(model.Name); if (uniqueCheck != string.Empty) { this.ModelState.AddModelError("Name", "分类名称不能为空"); } uniqueCheck = CategoryComp.GetCategoryName(model.CatID); if (uniqueCheck != string.Empty) { this.ModelState.AddModelError("CatID", "分类ID不能为空"); } if (this.ModelState.IsValid) { CategoryComp.Insert(model.CatID, model.Name); this.TempData["Message"] = "新建分类成功"; return this.RedirectToAction("Manage"); } } this.ViewData.Model = model; return this.View(); }
public ActionResult Edit(string id, CatModel model) { if (this.ModelState.IsValid) { if (!string.IsNullOrEmpty(id)) { Category oldCategory = CategoryComp.GetCategory(id); // get old category if (oldCategory.Name != model.Name) { if (!CategoryComp.IsUniqueName(model.Name)) { this.ModelState.AddModelError("Name", "分类名称不能为空"); } } if (id != model.CatID) { if (!CategoryComp.IsUniqueId(model.CatID)) { this.ModelState.AddModelError("CatID", "分类ID不能为空"); } } if (this.ModelState.IsValid) { Category cat2 = new Category(); cat2.CatID = model.CatID; cat2.Name = model.Name; cat2.Count = oldCategory.Count; cat2.Order = oldCategory.Order; CategoryComp.Update(oldCategory.CatID, cat2); } } } if (this.ModelState.IsValid) { this.TempData["Message"] = "分类信息已更新"; return this.RedirectToAction("Manage"); } else { this.ViewData.Model = model; return this.View(); } }
public ActionResult Edit(string id) { CatModel model = new CatModel(); model.CatID = id; Category cat = CategoryComp.GetCategory(id); // TempData["Category"] = cat; model.Name = cat.Name; this.ViewData.Model = model; return this.View(); }