public async Task <IActionResult> Edit(int?id, FlatViewImage pvm)
        {
            Flat flat = await _context.Flat.FindAsync(id);

            flat.OwnerName   = pvm.OwnerName;
            flat.PhoneNumber = pvm.PhoneNumber;
            flat.Description = pvm.Description;
            flat.Price       = pvm.Price;

            if (pvm.Image != null)
            {
                byte[] imageData = null;
                // считываем переданный файл в массив байтов
                using (var binaryReader = new BinaryReader(pvm.Image.OpenReadStream()))
                {
                    imageData = binaryReader.ReadBytes((int)pvm.Image.Length);
                }
                // установка массива байтов
                flat.Image = imageData;
            }
            if (id != null)
            {
                _context.Flat.Update(flat);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
                //flat = await _context.Flat.FirstOrDefaultAsync(p => p.RoomID == id);
                //if (flat != null)
                //    return View(flat);
            }
            return(NotFound());
        }
Esempio n. 2
0
        public async Task <IActionResult> PutDay(int id, Day day)
        {
            if (id != day.DayID)
            {
                return(BadRequest());
            }

            _context.Entry(day).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DayExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Register(ViewModels.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await db.User.FirstOrDefaultAsync(u => u.Login == model.Login);

                if (user == null)
                {
                    user = new User {
                        Name = model.Name, Login = model.Login, Number = model.Number, Password = model.Password
                    };
                    Role userRole = await db.Role.FirstOrDefaultAsync(r => r.Name == "user");

                    if (userRole != null)
                    {
                        user.Role = userRole;
                    }
                    db.User.Add(user);
                    await db.SaveChangesAsync();

                    await Authenticate(user);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Некоректні логін і(або) пароль");
                }
            }
            return(View(model));
        }