public ActionResult CreateEdit(int?id)
        {
            if (id == null) // Create
            {
                return(View(new UsersCreateEditVM()));
            }

            if (id > 0) // Edit
            {
                UserRepository userRepo = new UserRepository();

                User user = userRepo.GetById(id.Value);

                if (user == null)
                {
                    return(HttpNotFound());
                }

                UsersCreateEditVM u = new UsersCreateEditVM()
                {
                    ID        = user.ID,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Username  = user.Username,
                    Email     = user.Email,
                    IsAdmin   = user.IsAdmin
                };

                if (user.ImageName != null)
                {
                    u.ImageName = user.ImageName;
                }

                return(View(u));
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult CreateEdit(UsersCreateEditVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            User user;

            UserRepository userRepo = new UserRepository();

            if (model.ID > 0) // Edit
            {
                user = userRepo.GetById(model.ID);

                if (user == null)
                {
                    return(HttpNotFound());
                }
            }

            else // Create
            {
                user = new User();
            }

            if (model.Image != null && model.Image.ContentLength > 0)
            {
                if (model.Image.ContentType.Contains("image"))
                {
                    model.ImageName = Path.GetFileName(model.Image.FileName);
                    model.ImageName = model.Username + Path.GetExtension(model.ImageName);
                    string imagePath = Server.MapPath("~/Images/" + model.ImageName);
                    model.Image.SaveAs(imagePath);

                    user.ImageName = model.ImageName;
                }

                else
                {
                    ModelState.AddModelError(string.Empty, "Uploaded file isn't an image!");
                    return(View(model));
                }
            }

            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            user.Password  = model.Password;
            user.IsAdmin   = model.IsAdmin;

            bool userDoesntExist = true;

            if (model.Username != user.Username)
            {
                User userUsername = userRepo.GetAll(us => us.Username == model.Username).FirstOrDefault();

                if (userUsername != null)
                {
                    ModelState.AddModelError("Username", "Username already exists");
                    userDoesntExist = false;
                }

                user.Username = model.Username;
            }

            if (model.Email != user.Email)
            {
                User userEmail = userRepo.GetAll(us => us.Email == model.Email).FirstOrDefault();

                if (userEmail != null)
                {
                    ModelState.AddModelError("Email", "Email already exists");
                    userDoesntExist = false;
                }

                user.Email = model.Email;
            }

            if (userDoesntExist)
            {
                userRepo.Save(user);

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }