Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @using (Html.BeginForm("List", "Zamowienie", FormMethod.Post))
- {
- <input type="number" name="reserved">
- <input type="number" name="identy" value=@item.Id hidden="true"> // przekazujemy id towaru
- <input type="submit" value="Dodaj do zamówienia" class="btn btn-primary" />
- }
- //a metoda w controllerze wygląda tak
- [HttpPost]
- public ActionResult List(int? reserved, int? identy)
- {
- TempData["InvalidValueOfTowar"] = "";
- TempData["TooMuchOfTowar"] = "";
- Towar towar = db.Towary.SingleOrDefault(x => x.Id == identy.Value);
- if (towar != null)
- {
- if (reserved != null && reserved > 0)
- {
- if (reserved.Value > towar.Liczba)
- {
- TempData["TooMuchOfTowar"] = $"Brak takiej ilości towaru! Aktualnie znajduje się {towar.Liczba}.";
- }
- else
- {
- if (_pozycjeZamowienia.SingleOrDefault(x => x.TowarId == towar.Id) != null)
- {
- foreach (var pozycja in _pozycjeZamowienia)
- {
- if (pozycja.TowarId == towar.Id)
- {
- pozycja.iloscZamowionych += reserved.Value;
- pozycja.Cena = pozycja.iloscZamowionych * towar.CenaJNetto;
- }
- }
- }
- else
- {
- _pozycjeZamowienia.Add(new PozycjaZamowienia(towar, reserved.Value));
- }
- }
- }
- else
- TempData["InvalidValueOfTowar"] = "Nieprawidłowa liczba towaru!";
- }
- ViewBag.ListaZamowienia = _pozycjeZamowienia;
- return View(db.Towary);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement