Ejemplo n.º 1
0
        public ActionResult Edit(PhoneVersion phoneVersion,
                                 int PhoneId,
                                 string Version,
                                 decimal baseCost,
                                 HttpPostedFileBase imageFile,
                                 FormCollection form)
        {
            // Check for Newly Uploaded Image
            if (imageFile != null)
            {
                string path    = Server.MapPath("~/Images/Phones/");
                string webPath = webImageLocation;

                // Validate File Type Submitted
                var supportedFileTypes = new[] { "jpg", "bmp", "png" };
                var fileExt            = System.IO.Path.GetExtension(imageFile.FileName).Substring(1);
                if (supportedFileTypes.Contains(fileExt))
                {
                    UploadFile uf = new UploadFile();
                    uf.UploadFileToBlobStorage(imageFile, "phoneimages");

                    ViewBag.Message = "File uploaded successfully.";

                    // Update Model
                    phoneVersion.ImageName = imageFile.FileName;
                }
            }

            // Update Model
            phoneVersion.PhoneId  = PhoneId;
            phoneVersion.Version  = Version;
            phoneVersion.BaseCost = baseCost;
            phoneVersion.Active   = true;

            // Update Database
            db.Entry(phoneVersion).State = EntityState.Modified;
            db.SaveChanges();

            // Remove Previously Saved Version Carriers
            var removeVersionCarrier = db.VersionCarriers.Where(x => x.VersionId == phoneVersion.VersionId).ToList();

            if (removeVersionCarrier != null)
            {
                db.VersionCarriers.RemoveRange(removeVersionCarrier);
                db.SaveChanges();
            }

            foreach (var item in form)
            {
                string formItem = item.ToString();

                if (formItem.Contains("Carrier_"))
                {
                    var carrierId = item.ToString().Replace("Carrier_", "");
                    var value     = Request[formItem].ToString();

                    SaveVersionCarrier(phoneVersion.VersionId, int.Parse(carrierId.ToString()), int.Parse(value));
                }
            }

            // Remove Previously Saved Storage Capacities
            var removeVersionCapacity = db.VersionCapacities.Where(x => x.VersionId == phoneVersion.VersionId).ToList();

            if (removeVersionCapacity != null)
            {
                db.VersionCapacities.RemoveRange(removeVersionCapacity);
                db.SaveChanges();
            }

            // Insert Storage Capacities
            foreach (var item in form)
            {
                string formItem = item.ToString();
                if (formItem.Contains("Capacity_") && Request[formItem] != "")
                {
                    var id = Request[formItem];
                    id.Replace("Capacity_", "");
                    VersionCapacity versionCapacity = new VersionCapacity();
                    versionCapacity.StorageCapacityId = int.Parse(Request[formItem]);
                    versionCapacity.VersionId         = phoneVersion.VersionId;
                    versionCapacity.Value             = decimal.Parse(Request["Value_" + id].ToString());
                    db.VersionCapacities.Add(versionCapacity);
                    db.SaveChanges();
                }
            }

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public ActionResult Create([Bind(Include = "carrierId,VersionId,Version,BaseCost,ImageName")]
                                   PhoneVersion phoneVersion,
                                   int brandId,
                                   HttpPostedFileBase imageFile,
                                   FormCollection form)
        {
            if (imageFile != null)
            {
                // Validate File Type Submitted
                var supportedFileTypes = new[] { "jpg", "JPG", "png", "PNG", "gif", "GIF" };
                var fileExt            = System.IO.Path.GetExtension(imageFile.FileName).Substring(1);
                if (supportedFileTypes.Contains(fileExt))
                {
                    UploadFile uf = new UploadFile();
                    uf.UploadFileToBlobStorage(imageFile, "phoneimages");

                    ViewBag.Message = "File uploaded successfully.";

                    // Insert Model
                    phoneVersion.ImageName = imageFile.FileName;
                }
            }

            // Insert Phone Brand into Model
            phoneVersion.PhoneId = brandId;
            phoneVersion.Active  = true;

            // Save New Phone Version
            db.PhoneVersions.Add(phoneVersion);
            db.SaveChanges();

            try
            {
                // Create Defect Questions (Template)
                SqlParameter param1 = new SqlParameter("@NewVersionID", phoneVersion.VersionId);
                db.Database.ExecuteSqlCommand("CreateVersionDefectTemplate @NewVersionID",
                                              param1);
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex;
            }

            try
            {
                foreach (var item in form)
                {
                    string formItem = item.ToString();

                    if (formItem.Contains("Carrier_"))
                    {
                        var carrierId = item.ToString().Replace("Carrier_", "");
                        var value     = Request[formItem].ToString();

                        SaveVersionCarrier(phoneVersion.VersionId, int.Parse(carrierId.ToString()), int.Parse(value));
                    }
                }

                // Insert Storage Capacities
                foreach (var item in form)
                {
                    string formItem = item.ToString();

                    if (formItem.Contains("Capacity_") && Request[formItem] != "")
                    {
                        var id = Request[formItem];
                        id.Replace("Capacity_", "");
                        VersionCapacity versionCapacity = new VersionCapacity();
                        versionCapacity.StorageCapacityId = int.Parse(Request[formItem]);
                        versionCapacity.VersionId         = phoneVersion.VersionId;
                        versionCapacity.Value             = decimal.Parse(Request["Value_" + id].ToString());
                        db.VersionCapacities.Add(versionCapacity);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = ex;
            }

            return(RedirectToAction("Index"));
        }