Exemple #1
0
        public JsonResult UploadFile(int id)
        {
            string mediumPath="";
            try
            {
                foreach (string file in Request.Files)
                {
                    var fileContent = Request.Files[file];
                    if (fileContent != null && fileContent.ContentLength > 0)
                    {
                        var violationss = context.Violations.Find(id);
                        ViolationPicture oldPicture = violationss.ViolationPictures.FirstOrDefault();
                        context.ViolationPictures.Attach(oldPicture);
                        context.Entry(oldPicture).State = EntityState.Deleted;
                        context.SaveChanges();
                        ViolationPicture newViolationPicture = new ViolationPicture();
                        newViolationPicture = ImageHelper.SavePic(fileContent, HttpContext, newViolationPicture) as ViolationPicture;
                        violationss.ViolationPictures.Add(newViolationPicture);
                        context.SaveChanges();
                        mediumPath = newViolationPicture.MediumPath;
                    }
                }
            }
            catch (Exception)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return Json("Upload failed");
            }

            return Json(mediumPath,JsonRequestBehavior.AllowGet);
        }
        public ActionResult Create(CreateViolationViewModel createViolationViewModel)
        {
            int mainViolationId = Convert.ToInt32(createViolationViewModel.MainViolationType);
            int subViolationId = Convert.ToInt32(createViolationViewModel.SubViolationType);
            Violation violation = new Violation();
            violation.MainType = createViolationViewModel.MainViolationType;
            violation.SubType = createViolationViewModel.SubViolationType;
            violation.CreationDateTime = DateTime.Now;
            ViolationPicture violationPicture = new ViolationPicture();
            violationPicture = ImageHelper.SavePic(createViolationViewModel.VImage, HttpContext, violationPicture) as ViolationPicture;
            violation.ViolationPictures.Add(violationPicture);
            violation.Severity = createViolationViewModel.Severity;
            violation.Latitude = createViolationViewModel.Latitude;
            violation.Longtitude = createViolationViewModel.Longitude;
            violation.Title = createViolationViewModel.Title;
            violation.Description = createViolationViewModel.Description;
            violation.Address = createViolationViewModel.Address;
            violation.ViewCount = 0;
            violation.Status = Status.NotSolved;
            violation.ExternalMediaStorage = createViolationViewModel.ExternalMediaStorage;
            violation.MainViolationName = context.ViolationMainTypes.Where(x => x.Id == mainViolationId).FirstOrDefault().Name;
            violation.SubViolationName = context.ViolationSubTypes.Where(x => x.Id == subViolationId).FirstOrDefault().Name;
            var userID = User.Identity.GetUserId();
            if (createViolationViewModel.DynamicValue != null)
            {
                violation.HasDynamic = true;
                string[] stringArray = (string[])createViolationViewModel.DynamicValue;
                string dynamicValue = stringArray[0];
                violation.DynamicValue = dynamicValue;
                violation.DynamicName = createViolationViewModel.DynamicName;
            }
            else
            {
                violation.HasDynamic = false;
            }

            if (!string.IsNullOrEmpty(userID))
            {
                var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
                var currentUser = manager.FindById(User.Identity.GetUserId());
                violation.CreatorUser = currentUser;
            }

            context.Violations.Add(violation);
            context.SaveChanges();
            return RedirectToAction("Index", "Home");
        }