コード例 #1
0
 public ActionResult Edit(Category category)
 {
     if (ModelState.IsValid)
     {
         repository.SaveCategory(category);
         TempData["message"] = string.Format("{0} has been saved", category.CategoryCode);
         return RedirectToAction("Index");
     }
     else
     {
         // there is something wrong with the data values
         return View(category);
     }
 }
コード例 #2
0
        public void SaveCategory(Category category)
        {
            if (category.CategoryID == 0)
            {

                category.CreatedAt = DateTime.Now;
                category.UpdatedAt = DateTime.Now;
                context.Categories.Add(category);
            }
            else
            {
                Category dbEntry = context.Categories.Find(category.CategoryID);
                if (dbEntry != null)
                {
                    dbEntry.CategoryCode = category.CategoryCode;
                    dbEntry.Description = category.Description;
                    dbEntry.UpdatedAt = DateTime.Now;
                }
            }
            context.SaveChanges();
        }