public async Task <IActionResult> Create(HomeHeaderViewModel model)
        {
            if (model.HomeHeader.PhotoUpload != null)
            {
                try
                {
                    FileManager fileManager = new FileManager(webHostEnvironment);
                    model.HomeHeader.Photo = fileManager.Upload(model.HomeHeader.PhotoUpload);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("PhotoUpload", e.Message);
                }
            }
            if (ModelState.IsValid)
            {
                _context.Add(model.HomeHeader);
                await _context.SaveChangesAsync();

                foreach (var homeHeader in model.HomeHeaderTranslates)
                {
                    homeHeader.HomeHeaderId = _context.HomeHeaders.FirstOrDefault(x => x.Photo == model.HomeHeader.Photo && x.ModifiedAt == model.HomeHeader.ModifiedAt).Id;
                    _context.Add(homeHeader);
                    await _context.SaveChangesAsync();
                }
                TempData["Success"] = "Yeni Ana Səhifə Başlığı yaradıldl";
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductId"] = new SelectList(_context.Products, "Id", "Name", model.HomeHeader.ProductId);
            return(View(model));
        }
Exemple #2
0
        public PartialViewResult Header()
        {
            var username = GetCurrentUserName();
            var data     = new HomeHeaderViewModel();

            data.Account        = GetCurrentAccount();
            data.Notifications  = NotificationBLO.Current.GetByUser(username);
            data.NumberOfUnread = NotificationBLO.Current.CountUserUnread(username);
            return(PartialView(data));
        }
        public async Task <IActionResult> Edit(int id, HomeHeaderViewModel model)
        {
            if (id != model.HomeHeader.Id)
            {
                return(NotFound());
            }
            if (model.HomeHeader.PhotoUpload != null)
            {
                try
                {
                    FileManager fileManager = new FileManager(webHostEnvironment);
                    model.HomeHeader.Photo = fileManager.Upload(model.HomeHeader.PhotoUpload);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("HomeHeader_PhotoUpload", e.Message);
                }
            }
            if (ModelState.IsValid)
            {
                foreach (var item in model.HomeHeaderTranslates)
                {
                    item.HomeHeaderId = id;
                    _context.HomeHeaderTranslates.Update(item);
                }
                await _context.SaveChangesAsync();

                try
                {
                    model.HomeHeader.ModifiedAt = DateTime.Now;
                    _context.Update(model.HomeHeader);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!HomeHeaderExists(model.HomeHeader.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["Success"] = "Dəyişiklik uğurla başa çatdı";
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductId"] = new SelectList(_context.Products, "Id", "Name", model.HomeHeader.ProductId);
            return(View(model));
        }
        // GET: Control/HomeHeaders/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            HomeHeaderViewModel model = new HomeHeaderViewModel
            {
                HomeHeader           = await _context.HomeHeaders.FirstOrDefaultAsync(x => x.Id == id),
                HomeHeaderTranslates = _context.HomeHeaderTranslates.Include("Language").Where(x => x.HomeHeaderId == id).ToList()
            };

            if (model.HomeHeader == null)
            {
                return(NotFound());
            }
            ViewData["ProductId"] = new SelectList(_context.Products, "Id", "Name", model.HomeHeader.ProductId);
            return(View(model));
        }