コード例 #1
0
        public ActionResult Index()
        {
            ClickCounterModel model;

            using (var context = new ClickCounterContext())
            {
                model = context.ClickCounters.FirstOrDefault();
                if (model == null)
                {
                    model       = new ClickCounterModel();
                    model.Count = 0;
                    context.ClickCounters.Add(model);
                    context.SaveChanges();
                }
            }
            return(View(model));
        }
コード例 #2
0
 public ActionResult Count(ClickCounterModel model)
 {
     using (var context = new ClickCounterContext())
     {
         var dbmodel = context.ClickCounters.FirstOrDefault(x => x.Id == model.Id);
         dbmodel.Count += 1;
         if (TryValidateModel(dbmodel))
         {
             context.SaveChanges();
         }
         else
         {
             dbmodel.Count -= 1;
         }
         model = dbmodel;
     }
     return(View("Index", model));
 }