public async Task <ActionResult> Create(ListingForm model) { if (!ModelState.IsValid) { model.Accessories = _mapper.Map <List <KeyValueViewModel> > (await _unitOfWork.Accessories.GetAllAsync()); model.Availability.PopulateDaysOfWeek(); return(View(model)); } var userId = _userManager.GetUserId(HttpContext.User); var images = _mapper.Map <List <ListingImage> > (await _imageUploader.UploadImagesAsync(model.UploadedImages)); var listing = _mapper.Map <Listing>(model); listing.UserId = userId; listing.Images = images; await _unitOfWork.Listings.AddAsync(listing); _unitOfWork.Complete(); _unitOfWork.Listings.SetSpatialLocation(listing.Latitude, listing.Longitude, listing.Id); return(RedirectToAction("Details", new { id = listing.Id })); }
public Address(ListingForm UserInput) { addressLine1 = UserInput.addressLine1; addressLine2 = UserInput.addressLine2; city = UserInput.city; postalCode = UserInput.postalCode; state = UserInput.state; }
// GET: Listing/Create public async Task <ActionResult> Create() { var model = new ListingForm { Accessories = _mapper.Map <List <KeyValueViewModel> > (await _unitOfWork.Accessories.GetAllAsync()) }; return(View(model)); }
public IActionResult CreateListing(ListingForm UserInput) { if (ModelState.IsValid) { int? ID = HttpContext.Session.GetInt32("userid"); Broker user = _context.users .OfType <Broker>() .Where(use => use.UserId == ID) .FirstOrDefault(); if (user == null) { return(Redirect("/fail")); } //Add address to db Address NewAddress = new Address(UserInput); _context.address.Add(NewAddress); _context.SaveChanges(); //Add listing to db Listing NewListing = new Listing(UserInput, NewAddress); NewListing.Broker = user; NewListing.BrokerId = user.UserId; _context.listings.Add(NewListing); // add to broker's listings user.Listings.Add(NewListing); _context.SaveChanges(); return(Redirect("/listing-details/" + NewListing.ListingId)); } else { return(View("AddListing")); } }