public async Task <IActionResult> OnPostAsync() { int id = (int)HttpContext.Session.GetInt32("current_resturaunt"); Resturaunt R = await _context.Resturaunt.Include(m => m.ResturauntPage).ThenInclude(m => m.Days).FirstOrDefaultAsync(m => m.id == id); foreach (var i in R.ResturauntPage.Days) { _context.Entry(i) .Collection(s => s.Deals) .Load(); } var file = Request.Form.Files.First(); using (var reader = file.OpenReadStream()) { TextReader tr = new StreamReader(reader); using (var csv = new CsvReader(tr, CultureInfo.InvariantCulture)) { csv.Configuration.HasHeaderRecord = false; var deals = csv.GetRecords <CSVDeal>(); foreach (var d in deals) { Deal deal = new Deal() { ItemName = d.DealName, Desription = d.DealDescription, StartTime = d.StartTime, EndTime = d.EndTime }; _context.Deal.Add(deal); if (deal != null) { R.ResturauntPage.Days.Where(n => n.DayName.ToLower() == d.DayOfWeek.ToLower()).FirstOrDefault().Deals.Add(deal); } } _context.Attach(R).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { } } } return(Page()); }
public async Task OnGetAsync(int id, int day) { Tags = new List <Tags>(); Byte[] OutVal; if (!HttpContext.Session.TryGetValue("current_resturaunt", out OutVal)) { HttpContext.Session.SetInt32("current_resturaunt", id); } else { id = (int)HttpContext.Session.GetInt32("current_resturaunt"); } Resturaunt R = await _context.Resturaunt.Include(m => m.ResturauntPage).ThenInclude(m => m.Days).FirstOrDefaultAsync(m => m.id == id); Tags = _context.Tags.ToList(); SelectedTags = new int[Tags.Count]; switch (day) { case 1: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(0).id); Deals = Day.Deals; Day.DayName = "Monday"; break; case 2: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(1).id); Deals = Day.Deals; Day.DayName = "Tuesday"; break; case 3: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(2).id); Deals = Day.Deals; Day.DayName = "Wednesday"; break; case 4: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(3).id); Deals = Day.Deals; Day.DayName = "Thursday"; break; case 5: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(4).id); Deals = Day.Deals; Day.DayName = "Friday"; break; case 6: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(5).id); Deals = Day.Deals; Day.DayName = "Saturday"; break; case 7: Day = await _context.Day.Include(m => m.Deals).FirstOrDefaultAsync(m => m.id == R.ResturauntPage.Days.ElementAt(6).id); Deals = Day.Deals; Day.DayName = "Sunday"; break; } //preperation for tags foreach (var x in Day.Deals) { _context.Entry(x) .Collection(s => s.TagsInter) .Load(); } }