public IActionResult EditIdea(int Id, string IdeaTitle, string Description, string video, int Id1, string AllImagepath)
        {
            string[] imgToRemove = null;
            if (!string.IsNullOrEmpty(AllImagepath))
            {
                imgToRemove = AllImagepath.Split(',');
            }
            var postedIdea = _context.Idea.Where(x => x.ID == Id || x.ID == Id1).SingleOrDefault();
            var Imageidea  = _context.IdeaImages.Where(x => x.Id == Id1);

            if (imgToRemove != null && imgToRemove.Length > 0)
            {
                for (int i = 0; i < imgToRemove.Length; i++)
                {
                    var img = Imageidea.SingleOrDefault(x => x.Image.ToUpper() == imgToRemove[i].Trim().Substring(8).ToUpper());
                    _context.IdeaImages.Remove(img);
                }
                _context.SaveChanges();
            }
            ;

            foreach (IFormFile file in Request.Form.Files)
            {
                string fileName = Path.GetFileName(file.FileName);
                fileName = string.Format("{0}-{1}", Guid.NewGuid(), fileName);
                var webRoot = _env.WebRootPath;
                //var PathWithFolderName = System.IO.Path.Combine(webRoot, "MyFolder");
                var image = new IdeaImages();
                image.Image = fileName;
                image.Id    = Id1;
                _context.IdeaImages.Add(image);

                var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images", fileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    file.CopyToAsync(fileStream);
                }
            }


            if (IdeaTitle != null)
            {
                postedIdea.Title = IdeaTitle;
            }
            if (Description != null)
            {
                postedIdea.Description = Description;
            }
            if (video != null)
            {
                postedIdea.Video = video;
            }

            _context.Update(postedIdea);
            _context.SaveChanges();
            return(Json("success"));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(CreateIdeaModel model)
        {
            if (ModelState.IsValid)
            {
                Idea i = _context.Idea.FirstOrDefault(temp => temp.Title.ToLower().Trim() == model.Title.Trim().ToLower());
                if (i == null)
                {
                    i             = new Idea();
                    i.Description = model.Description;
                    i.PostDate    = DateTime.UtcNow;
                    i.Video       = model.Video;
                    if (HttpContext.User.Identities.Any(u => u.IsAuthenticated))
                    {
                        Member m = _context.Member.FirstOrDefault(temp => temp.Phone == HttpContext.User.Identity.Name);
                        if (m != null)
                        {
                            i.PostedBy = m;
                        }
                    }
                    i.Status = StatusType.Active;
                    i.Title  = model.Title;
                    _context.Add(i);
                    await _context.SaveChangesAsync();

                    foreach (IFormFile file in Request.Form.Files)
                    {
                        string fileName = Path.GetFileName(file.FileName);
                        var    webRoot  = _env.WebRootPath;
                        //var PathWithFolderName = System.IO.Path.Combine(webRoot, "MyFolder");
                        var image = new IdeaImages();
                        fileName = string.Format("{0}-{1}", Guid.NewGuid(), fileName);

                        image.Image = fileName;
                        image.Id    = i.ID;
                        _context.IdeaImages.Add(image);

                        var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images", fileName);
                        using (var fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await file.CopyToAsync(fileStream);
                        }

                        _context.SaveChanges();
                    }


                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("DuplicateTitle", "Another idea with same title is found.");
                }
            }
            return(View(model));
        }