예제 #1
0
        public ActionResult Create(YarnCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var service = CreateYarnService();

            if (service.CreateYarn(model))
            {
                TempData["SaveResult"] = "Your yarn was added.";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Yarn could not be added.");
            return(View(model));
        }
예제 #2
0
        public bool CreateYarn(YarnCreate model)
        {
            var entity =
                new Yarn()
            {
                UserId         = _userId,
                Color          = model.Color,
                Manufacturer   = model.Manufacturer,
                TotalYardage   = model.TotalYardage,
                TotalWeight    = model.TotalWeight,
                Fiber          = model.Fiber,
                WherePurchased = model.WherePurchased,
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Yarns.Add(entity);
                return(ctx.SaveChanges() == 1);
            }
        }