Exemplo n.º 1
0
        public ActionResult CompanyImagesList()
        {
            var user  = User as MPrincipal;
            var login = user.UserDetails.Login;

            ViewBag.UserName = user.UserDetails.Login;
            ViewBag.UserRole = 4;

            CompanyImagesVM model     = null;
            List <ImageVM>  imageList = new List <ImageVM>();


            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var company  = db.Companies.FirstOrDefault(u => u.Login.Equals(login));
                var dbImages = db.Images.Where(t => t.CompanyId == company.Id).ToList();

                model = new CompanyImagesVM();
                string mainPicture = "";
                foreach (var item in dbImages)
                {
                    if (item.IsDelete == false)
                    {
                        ImageVM imageVM = new ImageVM()
                        {
                            Id          = item.Id,
                            Description = item.Description,
                            Link        = item.Link
                        };
                        imageList.Add(imageVM);

                        if (item.MainPicture == true)
                        {
                            model.MainPicturePath = item.Link;
                            model.MainPictureID   = item.Id.ToString();
                        }
                    }
                }
                model.ImageList       = imageList;
                model.MainPicturePath = mainPicture;
            }


            return(View("CompanyImagesList", model));
        }
Exemplo n.º 2
0
        public ActionResult CompanyImagesList(CompanyImagesVM imagesModel)
        {
            var user  = User as MPrincipal;
            var login = user.UserDetails.Login;

            ViewBag.UserName = user.UserDetails.Login;
            ViewBag.UserRole = 4;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var company  = db.Companies.FirstOrDefault(u => u.Login.Equals(login));
                var dbImages = db.Images.Where(t => t.CompanyId == company.Id).ToList();
                foreach (var item in imagesModel.ImageList)
                {
                    foreach (var dbItem in dbImages)
                    {
                        if (dbItem.Id == item.Id)
                        {
                            dbItem.Link        = item.Link;
                            dbItem.Description = item.Description;
                            dbItem.CompanyId   = company.Id;
                            if (imagesModel.MainPictureID == dbItem.Id.ToString())
                            {
                                dbItem.MainPicture = true;
                            }
                            else
                            {
                                dbItem.MainPicture = false;
                            }
                        }
                    }
                    db.SaveChanges();
                }
                return(RedirectToAction("Index", "Home"));
            }
        }