public IActionResult SelectItemCount(int id)
 {
     try
     {
         AddingViewModel model = new AddingViewModel
         {
             ItemId    = id,
             ItemName  = repo.GetItemById(id).Name,
             ItemCount = 0
         };
         return(View(model));
     }
     catch (Exception)
     {
         return(RedirectToAction(nameof(Index)));
     }
 }
        public IActionResult SelectItemCount(AddingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                // there was a server-side validation error - return the user a new
                // form with what he put in.
                return(View(model));
            }

            if (items.FirstOrDefault(x => x.Id == model.ItemId) is Item item)
            {
                uint count = counts[item.Id] + model.ItemCount;
                if (count > 10)
                {
                    model.ItemCount = 10 - counts[item.Id];
                    return(View(model));
                }
            }

            try
            {
                locale.AddToOrder(client, model.ItemId, model.ItemCount, repo);
                if (items.FirstOrDefault(x => x.Id == model.ItemId) != null)
                {
                    counts[model.ItemId] += model.ItemCount;
                }
                else
                {
                    items.Add(repo.GetItemById(model.ItemId));
                    counts.TryAdd(model.ItemId, model.ItemCount);
                }
            }
            catch (ArgumentException ex)
            {
                //ModelState.AddModelError("Title", "title error");

                // empty string for "key" means, it's overall model error
                ModelState.AddModelError("", ex.Message);
                return(View(model));
            }

            return(RedirectToAction(nameof(Index)));
        }
Exemple #3
0
 public FormView()
 {
     InitializeComponent();
     DataContext = new AddingViewModel();
 }