Exemplo n.º 1
0
        public IActionResult AddJson(BudgetViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var result = BudgetManager.Add(model, CurrentUser);

                    return(Json(new
                    {
                        success = true,
                        Budget = result
                    }));
                }
            }
            catch (Exception e)
            {
                var innerMessage = e.InnerException == null ? "" : $": {e.InnerException.Message}";

                ModelState.AddModelError("", e.Message + innerMessage);
            }

            return(Json(new
            {
                success = false,
                errors = ModelState.Keys.SelectMany(k => ModelState[k].Errors)
                         .Select(m => m.ErrorMessage).ToArray()
            }));
        }
        public ActionResult Create(CreateBudgetViewModel model)
        {
            var householdId = User.Identity.GetHouseholdId();

            if (ModelState.IsValid && householdId != null)
            {
                _budgetManager.Add(new Budget {
                    HouseholdId = householdId.Value, Duration = model.Duration
                });
            }
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 3
0
        public IActionResult Add(BudgetViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var result = BudgetManager.Add(model, CurrentUser);

                    TempData["Success"] = $"Dodano nowy Budżet z numerem <b>{result.BudgetNumber}</b>";
                    //return RedirectToAction("Index");
                    return(RedirectToAction("Add"));
                }
            }
            catch (Exception e)
            {
                var innerMessage = e.InnerException == null ? "" : $": {e.InnerException.Message}";
                ModelState.AddModelError("", e.Message + innerMessage);
                //throw;
                TempData["Error"] = $"Wystąpił problem podczas dodawania nowego budżetu: {e.Message}{innerMessage}";
            }

            return(View(model));
        }