public AllLinkViewModel()
 {
     Bag    = new Bagsmvc();
     Person = new Peoplemvc();
     Link   = new Linksmvccore();
     People = new List <Peoplemvc>();
     Bags   = new List <Bagsmvc>();
     Links  = new List <Linksmvccore>();
 }
Пример #2
0
        private Linksmvccore UpdateZeroLinkNumber(Linksmvccore linksmvccore, List <Linksmvccore> alllinks)
        {
            Linksmvccore link = new Linksmvccore
            {
                BagId    = linksmvccore.BagId,
                PersonId = linksmvccore.PersonId
            };

            link.LinkNumber = alllinks
                              .Where(x => x.BagId == linksmvccore.BagId && x.PersonId == linksmvccore.PersonId)
                              .ToList()
                              .Max(z => z.LinkNumber);

            return(link);
        }
Пример #3
0
        public async Task <IActionResult> Create([Bind("LinkNumber,BagId,PersonId")] Linksmvccore linksmvccore)
        {
            if (ModelState.IsValid)
            {
                // Need to get links in order to update the list in memory
                var alllinks = await _cache.GetFromTable(_context.Links);

                // Prepare the database update
                _context.Links.Add(linksmvccore);

                // Perform the update
                await _context.SaveChangesAsync();

                // Have to refresh the cache
                await _cache.GetFromTable(true, _context.Links);

                // Go back to the bag controller to display just this bag
                return(RedirectToAction("Index", "Bags", new { id = linksmvccore.BagId }));
            }
            return(View(linksmvccore));
        }
Пример #4
0
        public async Task <ActionResult> Edit(BagViewModel bvm, IFormCollection collection, List <IFormFile> files)
        {
            if (ModelState.IsValid)
            {
                Boolean RefreshLinks = false;

                try
                {
                    Linksmvccore Link = new Linksmvccore();
                    // Not only do we update the bag, automatically create a link
                    // Only do this if a Person was selected AND if link doesn't already exist
                    if (bvm.Bag.PersonID != 0 && bvm.Bag.PersonID != null)
                    {
                        List <Linksmvccore> alllinks = await _cache.GetFromTable(_context.Links);

                        Linksmvccore link = alllinks.FirstOrDefault(x => x.BagId == bvm.Bag.Id && x.PersonId == (int)bvm.Bag.PersonID);
                        // No, the current link doesn't already exist, so save a new one
                        if (link == null)
                        {
                            Link.PersonId = (int)bvm.Bag.PersonID;
                            Link.BagId    = bvm.Bag.Id;
                            await _context.Links.AddAsync(Link);

                            RefreshLinks = true;
                        }
                    }

                    foreach (var file in files)
                    {
                        if (file != null || file.Length != 0)
                        {
                            String path = Path.Combine(
                                Directory.GetCurrentDirectory(), "wwwroot\\images",
                                file.FileName);

                            // This works with HostGator on mvc.fitpacking.com!!
                            //path = "wwwroot\\images\\" + file.FileName;

//                            return Content(path);
                            using (var stream = new FileStream(path, FileMode.Create))
                            {
                                await file.CopyToAsync(stream);
                            }
                        }
                    }

                    //TempData["debug"] = bvm.Bag.CopyFile(bvm.Bag.FrontFileName);
                    ////bvm.Bag.CopyFile(bvm.Bag.BackFileName);
                    ////bvm.Bag.CopyFile(bvm.Bag.BottomFileName);

                    // But mainly, we're here to update the bag
                    _context.Bags.Update(bvm.Bag);
                    await _context.SaveChangesAsync();

                    // Refresh the cache.  To update Navigation properties, an update of People is required as well
                    await _cache.GetFromTable(true, _context.Bags);

                    await _cache.GetFromTable(RefreshLinks, _context.Links);

                    await _cache.GetFromTable(RefreshLinks, _context.People);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception e)
                {
                    return(View());
                }
            }

            return(View(bvm.Bag));
        }