public async Task <IActionResult> Create([Bind("RestaurantDishRelationId,RestaurantId,DishId,Price,CreatedDateTime,LastModifiedDateTime")] RestaurantDishRelation restaurantDishRelation) { if (ModelState.IsValid) { restaurantDishRelation.CreatedDateTime = DateTime.Now; restaurantDishRelation.LastModifiedDateTime = DateTime.Now; _context.Add(restaurantDishRelation); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DishId"] = new SelectList(_context.Dish, "DishId", "Title", restaurantDishRelation.DishId); var selectList = _context.Restaurant .Include(r => r.Location) .ThenInclude(c => c.Street) .ThenInclude(c => c.District) .ThenInclude(c => c.City) .Select(c => new { RestaurantId = c.RestaurantId, Descr = $"{c.Title} (id:{c.RestaurantId}, {c.Location.Street.District.City.City1})" }); ViewData["RestaurantId"] = new SelectList(selectList, "RestaurantId", "Descr", restaurantDishRelation.RestaurantId); return(View(restaurantDishRelation)); }
public async Task <IActionResult> Edit(int id, [Bind("RestaurantDishRelationId,RestaurantId,DishId,Price,CreatedDateTime,LastModifiedDateTime")] RestaurantDishRelation restaurantDishRelation) { if (id != restaurantDishRelation.RestaurantDishRelationId) { return(NotFound()); } if (ModelState.IsValid) { try { if (restaurantDishRelation.CreatedDateTime.Year < 2000) { restaurantDishRelation.CreatedDateTime = DateTime.Now; } restaurantDishRelation.LastModifiedDateTime = DateTime.Now; _context.Update(restaurantDishRelation); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RestaurantDishRelationExists(restaurantDishRelation.RestaurantDishRelationId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["DishId"] = new SelectList(_context.Dish, "DishId", "Title", restaurantDishRelation.DishId); var selectList = _context.Restaurant .Include(r => r.Location) .ThenInclude(c => c.Street) .ThenInclude(c => c.District) .ThenInclude(c => c.City) .Select(c => new { RestaurantId = c.RestaurantId, Descr = $"{c.Title} (id:{c.RestaurantId}, {c.Location.Street.District.City.City1})" }); ViewData["RestaurantId"] = new SelectList(selectList, "RestaurantId", "Descr", restaurantDishRelation.RestaurantId); return(View(restaurantDishRelation)); }