public async Task <IActionResult> Create([Bind("BlogId,Title,ShortDescription,Description,ImageName")] Blog blog, IFormFile image) { var userId = _userService.GetUserIdByUserName(User.Identity.Name); blog.UserId = userId; blog.CreateDate = DateTime.Now; blog.Visit = 0; blog.IsDeleted = false; if (ModelState.IsValid) { if (image != null && image.Length > 0) { var fileName = Path.GetFileName(image.FileName); var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\BlogImages", fileName); await using (var fileSteam = new FileStream(filePath, FileMode.Create)) { await image.CopyToAsync(fileSteam); } blog.ImageName = fileName; } _context.Add(blog); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(blog)); }
public async Task <IActionResult> Create([Bind("EstateId,EstateTypeId,SubEstateType,Region,StatusId,CityId,Price,CreateDuration,Area,BedRooms,BathRooms,Floors,Address,ShortDescription,Description,Tags,Facilities")] Estate estate, IEnumerable <IFormFile> images) { var userId = _userService.GetUserIdByUserName(User.Identity.Name); estate.UserId = userId; estate.IsDeleted = false; estate.CreateDate = DateTime.Now; if (ModelState.IsValid) { _context.Add(estate); await _context.SaveChangesAsync(); if (images.Any()) { foreach (var files in images) { var fileName = Path.GetFileName(files.FileName); var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\EstateImages", fileName); await using (var fileSteam = new FileStream(filePath, FileMode.Create)) { await files.CopyToAsync(fileSteam); } EstateImage estateImage = new EstateImage() { EstateId = estate.EstateId, Image = fileName }; await _context.EstateImages.AddAsync(estateImage); } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["CityId"] = new SelectList(_context.Cities, "CityId", "CityName", estate.CityId); ViewData["EstateTypeId"] = new SelectList(_context.EstateTypes, "EstateTypeId", "Type", estate.EstateTypeId); // ViewData["SubCities"] = new SelectList(_context.EstateTypes, "EstateTypeId", "Type", estate.EstateTypeId); // ViewData["EstateTypeId"] = new SelectList(_context.EstateTypes, "EstateTypeId", "Type", estate.EstateTypeId); return(View(estate)); }
public int AddUser(User user) { _context.Add(user); _context.SaveChanges(); return(user.UserId); }