public async Task <IActionResult> Create([Bind("LocationId,DateCreated,Description,LocationName,StreetAddress,UserId,CityId,LocationTypeId, WebsiteURL, PaymentTypeLocation")] Location location, string[] selectedPaymentTypes) { { location.PaymentTypeLocations = new List <PaymentTypeLocation>(); foreach (var paymentType in selectedPaymentTypes) { var paymentToAdd = new PaymentTypeLocation { LocationId = location.LocationId, PaymentTypeId = int.Parse(paymentType) }; location.PaymentTypeLocations.Add(paymentToAdd); } } ModelState.Remove("User"); ModelState.Remove("UserId"); if (ModelState.IsValid) { var CurrentUser = await GetCurrentUserAsync(); location.UserId = CurrentUser.Id; _context.Add(location); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedPaymentTypeData(location); ViewData["CityId"] = new SelectList(_context.Cities, "CityId", "CityName", location.CityId); ViewData["LocationTypeId"] = new SelectList(_context.LocationTypes, "LocationTypeId", "LocationTypeName", location.LocationTypeId); return(View(location)); }
//Special method that updates the join tables on the Location being updated in the Edit view of MyLocation private void UpdatePaymentTypeLocationData(string[] selectedPaymentTypes, Location locationToUpdate) { if (selectedPaymentTypes == null) { locationToUpdate.PaymentTypeLocations = new List <PaymentTypeLocation>(); return; } var selectedPaymentTYpesHS = new HashSet <string>(selectedPaymentTypes); var preEditPaymentTypes = new HashSet <int> (locationToUpdate.PaymentTypeLocations.Select(c => c.PaymentType.PaymentTypeId)); foreach (var paymentType in _context.PaymentType) { if (selectedPaymentTYpesHS.Contains(paymentType.PaymentTypeId.ToString())) { if (!preEditPaymentTypes.Contains(paymentType.PaymentTypeId)) { locationToUpdate.PaymentTypeLocations.Add(new PaymentTypeLocation { LocationId = locationToUpdate.LocationId, PaymentTypeId = paymentType.PaymentTypeId }); } } else { if (preEditPaymentTypes.Contains(paymentType.PaymentTypeId)) { PaymentTypeLocation paymentTypeToRemove = locationToUpdate.PaymentTypeLocations.FirstOrDefault(i => i.PaymentTypeId == paymentType.PaymentTypeId); _context.Remove(paymentTypeToRemove); } } } }
public static void Initialize(ApplicationDbContext context) { context.Database.EnsureCreated(); // Look for any join table. if (context.PaymentTypeLocations.Any()) { return; // DB has been seeded } var city = new City[] { new City { CityName = "Huntington" }, new City { CityName = "Charleston" }, new City { CityName = "New York" }, new City { CityName = "Los Angeles" }, new City { CityName = "Chicago" }, new City { CityName = "Wheeling" }, }; foreach (City s in city) { context.Cities.Add(s); } context.SaveChanges(); var locationType = new LocationType[] { new LocationType { LocationTypeName = "Bar" }, new LocationType { LocationTypeName = "Restaurant" }, new LocationType { LocationTypeName = "Grocery" }, new LocationType { LocationTypeName = "Retail Outlet" }, new LocationType { LocationTypeName = "Entertainment" }, new LocationType { LocationTypeName = "Lodging" }, new LocationType { LocationTypeName = "Other, please specify in description" }, }; foreach (LocationType c in locationType) { context.LocationTypes.Add(c); } context.SaveChanges(); var paymentType = new PaymentType[] { new PaymentType { PaymentTypeName = "Bitcoin", PaymentTypeTicker = "BTC" }, new PaymentType { PaymentTypeName = "Ethereum", PaymentTypeTicker = "ETH" }, new PaymentType { PaymentTypeName = "Bitcoin Cash", PaymentTypeTicker = "BCH" }, new PaymentType { PaymentTypeName = "Bitcoin Satoshi's Vision", PaymentTypeTicker = "BSV" }, new PaymentType { PaymentTypeName = "Ripple", PaymentTypeTicker = "XRP" }, new PaymentType { PaymentTypeName = "Zcash", PaymentTypeTicker = "ZEC" }, }; foreach (PaymentType e in paymentType) { context.PaymentType.Add(e); } context.SaveChanges(); var paymentLocation = new PaymentTypeLocation[] { new PaymentTypeLocation { PaymentTypeId = 2, LocationId = 2 }, new PaymentTypeLocation { PaymentTypeId = 1, LocationId = 2 }, new PaymentTypeLocation { PaymentTypeId = 3, LocationId = 3 }, }; foreach (PaymentTypeLocation ci in paymentLocation) { context.PaymentTypeLocations.Add(ci); } context.SaveChanges(); }