public static IngredientBasicViewModel MapToViewModel(this IngredientBasicDTO ingredient) { var vm = new IngredientBasicViewModel { Id = ingredient.Id, Name = ingredient.Name, Unit = ingredient.Unit, CanDelete = ingredient.CanDelete }; return(vm); }
public async Task <IActionResult> Add([FromBody] IngredientBasicViewModel vm) { var action = TempData["Action"].ToString(); if (ModelState.IsValid) { try { var ingredient = (await ingredientServices.AddAsync(vm.Name, vm.Unit)).MapToViewModel(); return(Json(ingredient)); } catch (ArgumentException ex) { TempData["Status"] = ex.Message; return(RedirectToAction(action, "Cocktails")); } } TempData["Status"] = "Invalid ingredient parameters"; return(RedirectToAction(action, "Cocktails")); }