public async Task <IActionResult> Create([Bind("Id,Title,Description,AddDate,ExpirationDate,UserIdVal,CategoryIdVal")] Ad ad, IFormFile image) { if (ModelState.IsValid) { using (var stream = new MemoryStream()) { await image.CopyToAsync(stream); ad.Image = stream.ToArray(); } ad.AddDate = DateTime.Now; ad.ExpirationDate = ad.AddDate.AddDays(7); //ad.User = _context.Users.FirstOrDefault(e => e.Id == ad.UserIdVal); if (CurrentUser.User != null) { ad.User = _context.Users.FirstOrDefault(e => e.Username == CurrentUser.User.Username); } ad.Category = _context.Categories.FirstOrDefault(e => e.Id == ad.CategoryIdVal); _context.Add(ad); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["Categories"] = new SelectList(_context.Categories, "Id", "Id", ad.CategoryIdVal); //ViewData["Users"] = new SelectList(_context.Users, "Id", "Id", ad.UserIdVal); return(View(ad)); }
public async Task <IActionResult> Create([Bind("Id,CategoryName")] Category category) { if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(category)); }
public async Task <IActionResult> Create(int?id, [Bind("CommentID,UserId,AdvertisementID,DateOfComent,Text,Like,Dislike")] Comment comment) { //if (ModelState.IsValid) //{ comment.UserId = _userName; comment.AdvertisementID = (int)id; comment.DateOfComent = DateTime.Now; comment.Like = 0; comment.Dislike = 0; _context.Add(comment); await _context.SaveChangesAsync(); // return RedirectToAction("Index"); //} var comments = _context.Comments; var model = new CommentViewModel(comments); return(View("~/Views/Comments/Index.cshtml", model)); }
public async Task <IActionResult> Create([Bind("ID,Name,Description,Price,CategoryId")] Advertisement advertisement) { if (ModelState.IsValid) { _context.Add(advertisement); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "ID", "Name", advertisement.CategoryId); return(View(advertisement)); }
public async Task <IActionResult> Create([Bind("Id,Title,Description,AddDate,ExpirationDate")] Ad ad) { if (ModelState.IsValid) { ad.AddDate = DateTime.Now; ad.ExpirationDate = ad.AddDate.AddDays(7); _context.Add(ad); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(ad)); }
[ValidateAntiForgeryToken] //"ID,UserId,City,Area,Address,Description,Studio,OneRoom,TwoRooms,ThreeRooms,Price,Contact,Date, Apartment public async Task <IActionResult> Create([Bind("ID,UserId,City,Area,Address,Description,Studio,OneRoom,TwoRooms,ThreeRooms,Price,Contact,Date")] Advertisement advertisement) { if (ModelState.IsValid) { advertisement.UserID = _userName; advertisement.Date = DateTime.Now; _context.Add(advertisement); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewData["UserID"] = new SelectList(_context.Users, "ID", "ID", advertisement.UserID); return(View(advertisement)); }
public async Task <IActionResult> Create([Bind("ID, City,Area,Address,Description,Studio,OneRoom,TwoRooms,ThreeRooms,Price,Contact,Date")] PutAdViewModel putad, IFormFile upload) { //if (ModelState.IsValid) //{ // _context.Add(putAdViewModel); // await _context.SaveChangesAsync(); // return RedirectToAction("Index"); //} //return View(putAdViewModel); if (ModelState.IsValid) { //if (upload != null && upload.Length > 0) //{ // //Models.File fileIn = new Models.File(); // Data.EfModels.UploadImage Imagein = new UploadImage(); // var fileName = ContentDispositionHeaderValue.Parse(upload.ContentDisposition).FileName.Trim('"'); // byte[] fileBytes = null; // using (var fileStream = upload.OpenReadStream()) // using (var ms = new MemoryStream()) // { // fileStream.CopyTo(ms); // fileBytes = ms.ToArray(); // //string s = Convert.ToBase64String(fileBytes); // // act on the Base64 data // } // Imagein.Imagename = fileName; // Imagein.ContentType = upload.ContentType; // Imagein.Content = fileBytes; // _context.Add(Imagein); // await _context.SaveChangesAsync(); // _context.Add(Imagein); //} _context.Add(putad); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } //ViewData["ID"] = new SelectList(_context.Users, "ID", "ID", advertisement.ID); return(View(putad)); }
public async Task Create([FromBody] Ad ad) { _context.Add(ad); await _context.SaveChangesAsync(); }