-1

I'm trying post data from View to controller and then return back to view to get feedback from controller. This is my script in view.

<script type="text/javascript">
$(document).ready(function () { 
    $("Submit").click(function (e) {
        e.preventDefault();

        var token = $('input[name=__RequestVerificationToken]').val();
        console.log(token);

        var dataToPost = $(this).serialize();
        $.ajax({
            url: "Process/Payment"
            , method: "POST"
            , data: {
                __RequestVerificationToken: token
                , page: dataToPost
            }
            , done: function (data) {
                window.location.replace (data.newUrl);
                alert(data);
            }
            , error: function () {
                alert("Ajax call failed");

            }
        })
    })
})

and this is my controller. All data that wrap in 'Payment' model is in good shape, pass well from view to controller. And, I want to return back to view (url: "Process/Payment")

[HttpPost]
public ActionResult Allowance (Allowance allowance) {
    return Json(new {
            newUrl = Url.Action("Payment", "Process")
        }
    );

but when the page return to localhost, I just get this page.

How should I do to return the page that look like before I hit submit button as POST ?

why "window.location.replace ()" can't redirect page to the original page ?

Thx's

Daleman
  • 794
  • 8
  • 23
  • How about using window.location.href = "URL" – Vijay Mishra May 07 '18 at 10:21
  • You are already in view page. aren't you? – Mithu CN May 07 '18 at 10:21
  • Why in the world are you making an ajax call when you want to redirect (it makes no sense at all - just make a normal submit) –  May 07 '18 at 10:51
  • And there is no such html element as `` so none of the code in you function will ever be executed (you should not be writing scripts without knowing at least the basics of how to debug them) - I assume you mean `$("#Submit")` assuming you have an element with `id="Submit"` –  May 07 '18 at 10:54
  • Hi all, I made mistake by putting this url: "Process/Payment". This should be the action method only without controller. Thank's All. – Daleman May 08 '18 at 01:47
  • Why have you accepted an answer that has nothing to do with your issue (it just misleads other users) –  May 08 '18 at 08:52

1 Answers1

-1

Just add a window.location.href=="Your Desired Page"; on the success event of your ajax call. That'll do the thing.

Happy Coding.

Maaz Ali
  • 83
  • 5