0

I have question about passing values with ajax. I have done something like this:

 $('#zlozZamowienie').click(function () {
    var wycieczki = [];
    $("input[name=Id_wycieczki]:checked").each(function () {
        var id = $(this).attr('id');
        wycieczki.push(id);
    });


    var productModel = {

        Id_oferty: $("#Id_oferty").val(),
        Nazwa_oferty: $("#Nazwa_oferty").val(),
        Data_od: $("#Data_od").val(),
        Data_do: $("#Data_do").val(),
        Cena_za_miejsce: $("#Cena_za_miejsce").val(),
        iloscDni: $("#iloscDni").val(),
        SelectedKwaterunek: $("input:radio[name=SelectedKwaterunek]:checked").val(),
        IdWycieczek: wycieczki


    };

    $.ajax({
        type: "POST",
        url: '@Url.Action("Szczegoly", "Oferta")',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ 'skomponowanaOferta': productModel}),

        dataType: "json",
        success: function () {
            alert('Success');
        },
        error: function (xhr) {
            alert(xhr.error);
        }

    });
    return false;




});

Szczegoly controller

  [HttpPost]
    public ActionResult Szczegoly(SzczegolyOfertyViewModel skomponowanaOferta)
    {

        if (ModelState.IsValid) { 
           ...
            };
            TempData["Szczegoly"] = szczegoly; 
            return RedirectToAction("ZlozZamowienie", "Zamowienia");
        }

Zamowienia Controller

   public ActionResult ZlozZamowienie()
    {

        var skomponowanaOferta = (SzczegolyOfertyViewModel) TempData["Szczegoly"]; 
        SzczegolyOfertyViewModel podsumowanie = new SzczegolyOfertyViewModel();

        if (SprawdzWycieczki(skomponowanaOferta.IdWycieczek))
        {
           ...

           };

            return View(podsumowanie);
        }

Which is passing data correctly but because of this return false statement my button do not redirect me anywhere. When I change this return to true or just delete it, this ajax pass nulls. What am I doing wrong?

EDIT: I have error from ajax like this: enter image description here

TjDillashaw
  • 787
  • 1
  • 12
  • 29

1 Answers1

0

Ok I found answer here RedirectToAction not working after successful jquery ajax post?

and then I add this to my ajax:

location.href="url"

like as @vinayakj said

Community
  • 1
  • 1
TjDillashaw
  • 787
  • 1
  • 12
  • 29
  • Why on earth are you not just serializing your form - data: $('form').serialize()` and remove the `contentType` option. But using ajax when you actually want to redirect is just plain crazy and pointless extra overhead. Just do a normal submit! –  Dec 06 '15 at 21:46
  • easy, Im just trying to learn new things. Thank you for this advice! – TjDillashaw Dec 07 '15 at 07:44