Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. @using (Html.BeginForm("List", "Zamowienie", FormMethod.Post))
  2.                 {
  3.                     <input type="number" name="reserved">
  4.                     <input type="number" name="identy" value=@item.Id hidden="true"> // przekazujemy id towaru
  5.  
  6.                     <input type="submit" value="Dodaj do zamówienia" class="btn btn-primary" />
  7.                 }
  8.  
  9. //a metoda w controllerze wygląda tak
  10.  
  11. [HttpPost]
  12.         public ActionResult List(int? reserved, int? identy)
  13.         {
  14.             TempData["InvalidValueOfTowar"] = "";
  15.             TempData["TooMuchOfTowar"] = "";
  16.             Towar towar = db.Towary.SingleOrDefault(x => x.Id == identy.Value);
  17.             if (towar != null)
  18.             {
  19.                 if (reserved != null && reserved > 0)
  20.                 {
  21.                     if (reserved.Value > towar.Liczba)
  22.                     {
  23.                         TempData["TooMuchOfTowar"] = $"Brak takiej ilości towaru! Aktualnie znajduje się {towar.Liczba}.";
  24.                     }
  25.                     else
  26.                     {
  27.                         if (_pozycjeZamowienia.SingleOrDefault(x => x.TowarId == towar.Id) != null)
  28.                         {
  29.                             foreach (var pozycja in _pozycjeZamowienia)
  30.                             {
  31.                                 if (pozycja.TowarId == towar.Id)
  32.                                 {
  33.                                     pozycja.iloscZamowionych += reserved.Value;
  34.                                     pozycja.Cena = pozycja.iloscZamowionych * towar.CenaJNetto;
  35.                                 }
  36.                             }
  37.                         }
  38.                         else
  39.                         {
  40.                             _pozycjeZamowienia.Add(new PozycjaZamowienia(towar, reserved.Value));
  41.                         }
  42.  
  43.                     }
  44.                 }
  45.                 else
  46.                     TempData["InvalidValueOfTowar"] = "Nieprawidłowa liczba towaru!";
  47.             }
  48.            
  49.             ViewBag.ListaZamowienia = _pozycjeZamowienia;
  50.             return View(db.Towary);
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement