コード例 #1
0
        public ActionResult Edit(int id, Models.Hammer hammer)
        {
            if (!ModelState.IsValid) // It's valid even when user = null
            {
                return(View());
            }

            try
            {
                Models.Hammer oldHammer = context.Hammers.FirstOrDefault(x => x.Id == id);
                oldHammer.Name        = hammer.Name;
                oldHammer.Description = hammer.Description;
                oldHammer.Stock       = hammer.Stock;
                oldHammer.Cost        = hammer.Cost;
                oldHammer.UpdatedOn   = System.DateTime.Now;

                context.Hammers.Attach(oldHammer);
                context.Entry(oldHammer).State = System.Data.Entity.EntityState.Modified;
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
コード例 #2
0
        public ActionResult Add(Models.Hammer hammer)
        {
            if (!ModelState.IsValid) // It's valid even when user = null
            {
                return(View());
            }

            try
            {
                hammer.CreatedOn = System.DateTime.Now;
                hammer.UpdatedOn = System.DateTime.Now;

                context.Hammers.Add(hammer);
                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
コード例 #3
0
 // GET: Home/Edit/5
 public ActionResult Edit(int id)
 {
     Models.Hammer hammer = context.Hammers.FirstOrDefault(x => x.Id == id);
     return(View(hammer));
 }