public async Task <IActionResult> Index([Bind("InfoId,Email,Address,PhoneNumber,WorkingTime,Facebook,Twitter,Instagram,Pinterest,Linkedin")] Information information)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(information);
                    await _context.SaveChangesAsync();

                    TempData["Notice"] = "Lưu thông tin thành công";
                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!InformationExists(information.InfoId))
                    {
                        return(NotFound());
                    }
                }
            }

            TempData["Notice"] = "Lỗi: Không thể lưu thông tin, vui lòng thử lại sau.";
            return(View(information));
        }
示例#2
0
        public async Task <IActionResult> Edit(int id, [Bind("CustomerId,Firstname,LastName,Email,Address,PhoneNumber,AccountId,AvatarUrl,ModifiedDate")] Customer customer,
                                               IFormFile file = null)
        {
            if (id != customer.CustomerId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null && file.Length > 0)
                    {
                        // Delete old image file
                        var oldPath = Directory.GetCurrentDirectory() + @"\wwwroot\images\avatars\" + customer.AvatarUrl;

                        if (System.IO.File.Exists(oldPath) && customer.AvatarUrl != "noAvatar.png")
                        {
                            System.IO.File.Delete(oldPath);
                        }

                        // Add new image file
                        string fileName = Path.GetFileName(file.FileName);

                        string extensionFileName = Path.GetExtension(fileName);

                        fileName = fileName.Substring(0, fileName.Length - extensionFileName.Length) + "-" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("/", "") + extensionFileName;

                        var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images\avatars", fileName);

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

                        customer.AvatarUrl = fileName;
                    }

                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerId))
                    {
                        TempData["Notice"] = "Lỗi: Không thể lưu thông tin, vui lòng thử lại sau.";
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                TempData["Notice"] = "Lưu thông tin của " + customer.LastName + " " + customer.Firstname + " thành công";
                return(RedirectToAction(nameof(Index)));
            }

            return(View(customer));
        }