Example #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CMCContext(
                       serviceProvider.GetRequiredService <DbContextOptions <CMCContext> >()))
            {
                if (context.Products.Any())
                {
                    return;
                }

                context.Products.AddRange(
                    new Product
                {
                    ProductId          = 1,
                    ProductName        = "Cricket Bat",
                    ProductDescription = "Wooden bat. Do you like that?",
                    Price    = 249.99,
                    ImageURL = "/Images/bat.jpg"
                }, new Product
                {
                    ProductId          = 2,
                    ProductName        = "Cricket Ball",
                    ProductDescription = "Leather ball. Will make you fall.",
                    Price    = 9.99,
                    ImageURL = "/Images/ball.png"
                }
                    , new Product
                {
                    ProductId          = 3,
                    ProductName        = "Cricket Pad",
                    ProductDescription = "Leather Pad. You wish you always had.",
                    Price    = 129.99,
                    ImageURL = "/Images/pad.png"
                }, new Product
                {
                    ProductId          = 4,
                    ProductName        = "Cricket Gloves",
                    ProductDescription = "Premium Gloves. Everyone loves.",
                    Price    = 11.99,
                    ImageURL = "/Images/gloves.jpg"
                }, new Product
                {
                    ProductId          = 5,
                    ProductName        = "Cricket Stumps",
                    ProductDescription = "Made of wood and steel. You won't get a better deal.",
                    Price    = 149.99,
                    ImageURL = "/Images/stumps.jpg"
                }, new Product
                {
                    ProductId          = 6,
                    ProductName        = "Cricket Helmet",
                    ProductDescription = "Cricket Helmet. ... sorry we ran out of rhymes here...",
                    Price    = 29.99,
                    ImageURL = "/Images/helmet.jpg"
                });

                context.SaveChanges();
            }
        }
Example #2
0
        public ActionResult Create([Bind(Include = "WON,Name,Description,StartDate,EndDate,Status")] Project project)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Projects.Add(project);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return(View(project));
        }
 public ActionResult Create([Bind(Include = "ContractID,Name,JobID")] Contract contract)
 {
     try
     {
         if (ModelState.IsValid)
         {
             db.Contracts.Add(contract);
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
     }
     catch (RetryLimitExceededException /* dex */)
     {
         //Log the error
         ModelState.AddModelError("", "Unable to save changes. Try again.");
     }
     PopulateJobsDropDownList(contract.JobID);
     return(View(contract));
 }
        public ActionResult Create([Bind(Include = "EmployeeID,FirstName,LastName,Email,PhoneNumber,OfficeNumber,Type,Status")] Employee employee, string[] selectedContracts)
        {
            if (selectedContracts != null)
            {
                employee.Contracts = new List <Contract>();
                foreach (var contract in selectedContracts)
                {
                    var contractToAdd = db.Contracts.Find(int.Parse(contract));
                    employee.Contracts.Add(contractToAdd);
                }
            }



            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            PopulateAssignedContractData(employee);
            return(View(employee));
        }
        public ActionResult Create(Photo photo, IEnumerable <HttpPostedFileBase> files)
        {
            if (!ModelState.IsValid)
            {
                return(View(photo));
            }
            if (files.Count() == 0 || files.FirstOrDefault() == null)
            {
                ViewBag.error = "Please choose a file";
                return(View(photo));
            }

            var model = new Photo();

            foreach (var file in files)
            {
                if (file.ContentLength == 0)
                {
                    continue;
                }

                model.Description = photo.Description;
                var fileName  = Guid.NewGuid().ToString();
                var extension = System.IO.Path.GetExtension(file.FileName).ToLower();

                using (var img = System.Drawing.Image.FromStream(file.InputStream))
                {
                    model.ThumbPath = String.Format("/GalleryImages/thumbs/{0}{1}", fileName, extension);
                    model.ImagePath = String.Format("/GalleryImages/{0}{1}", fileName, extension);

                    // Save thumbnail size image, 100 x 100
                    SaveToFolder(img, fileName, extension, new Size(100, 100), model.ThumbPath);

                    // Save large size image, 800 x 800
                    SaveToFolder(img, fileName, extension, new Size(800, 800), model.ImagePath);
                }

                // Save record to database
                model.CreatedOn = DateTime.Now;
                db.Photos.Add(model);
                db.SaveChanges();
            }

            return(RedirectPermanent("/Photos"));
        }