// GET: Categories/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Category category = _db.Categories.Get(id); if (category == null) { return(HttpNotFound()); } var categoryView = new CreateCategoriesViewModel { Name = category.Name, ParentCatId = category.ParentCatId, Categories = _db.Categories.GetAll(x => x.IsDeleted == false), CategoryId = category.CategoryId, CreationTime = category.CreationTime, CreatedBy = category.CreatedBy }; var catParentName = _db.Categories.Get(category.ParentCatId)?.Name; ViewBag.catParentName = catParentName; return(View(categoryView)); }
// GET: Categories/Create public ActionResult Create() { //ViewBag.ParentCatId = new SelectList(_db.Categories.GetAll(x => x.IsDeleted == false), // "CategoryId", "Name"); var categories = _db.Categories.GetAll(x => x.IsDeleted == false); var categoryView = new CreateCategoriesViewModel() { Categories = categories }; return(View(categoryView)); }
public ActionResult Edit([Bind(Include = @"CategoryId,ParentCatId,Name,CreatedBy,CreationTime,UpdatedTime")] CreateCategoriesViewModel categoryView) { if (ModelState.IsValid) { var category = _db.Categories.Get(categoryView.CategoryId); category.CategoryId = categoryView.CategoryId; category.Name = categoryView.Name; category.ParentCatId = categoryView.ParentCatId; category.CreatedBy = categoryView.CreatedBy; category.CreationTime = category.CreationTime; category.UpdatedBy = User.Identity.Name; category.UpdatedTime = DateTime.Now; category.IsDeleted = false; _db.Save(); return(RedirectToAction("Index")); } return(View(categoryView)); }
public ActionResult Create(CreateCategoriesViewModel model) { try { if (ModelState.IsValid) { var category = new Categories { Name = model.Name }; repository.Add(category); return(RedirectToAction("index")); } return(View(model)); } catch { return(View()); } }
public ActionResult Create([Bind(Include = "CategoryId,ParentCatId,Name")] Category category) { if (ModelState.IsValid) { category.IsDeleted = false; category.CreationTime = DateTime.Now; category.CreatedBy = User.Identity.Name; _db.Categories.Add(category); _db.Save(); return(RedirectToAction("Index")); } var categoryView = new CreateCategoriesViewModel { Name = category.Name, ParentCatId = category.ParentCatId, Categories = _db.Categories.GetAll(x => x.IsDeleted == false) }; return(View(categoryView)); }