0

I'm sending some json data with ajax by post

function SendF() {

$.ajax({
    url: '@Url.Action("Summary")',
    type: 'POST',
    data: JSON.stringify(flags),
    contentType: "application/json;charset=utf-8",
    success: function() {

    },
    error: function() {
        alert("Oops! We've experienced a connection problem!");
    }
});
}

to my controller

 [HttpPost]
        public ActionResult Summary(List<string> flagsChecked)
        {

             [...]

                return View(flags);
        }

and tried returning a view with data I've processed, but I guess it's not gonna happen since ajax is all about asynchronous http requests. How do I change my code to be synchronous?

Denis Wasilew
  • 503
  • 3
  • 9
  • 29
  • 2
    Making an ajax call and then using `window.location.href` is pointless. Your method needs to return a partial view and then you update some part of the DOM in the callback e.g. `success: function(data) { $(someElement).html(data); }` –  Nov 10 '17 at 21:19
  • I've realised that but pasted the old code. Sorry. Anyways, I do prefer changing it not to be asynchronous, so I won't have to change the whole view for this as you describe – Denis Wasilew Nov 10 '17 at 21:23
  • Sorry, your last comment makes no sense - it has nothing to do with asynchronous or synchronous –  Nov 10 '17 at 21:25
  • Can you elaborate what you mean by " but I guess it's not gonna happen " ??? – Shyju Nov 10 '17 at 21:27
  • Uhm... I mean to just simply redirect to the view from the controller after I process the data. So I should switch the first ajax post call to a simple post call (but don't know which one, and how yet)? – Denis Wasilew Nov 10 '17 at 21:30
  • You want to redirect to a new action method when the ajax call is done ? You can do `window.location.href="yourNewUrl";` inside the `success` handler of the ajax call. But it sorts of defeats the purpose of ajax. Also why are you returning the view result if you are planing to do a client side redirection later ? Why not do a simple form post without ajax ? – Shyju Nov 10 '17 at 21:31
  • That's pretty much what I was trying to say. I just don't know how to change it to non-ajax post. (I've done that by ajax because in the past I was doing some things with ajax post, but then I realized that in this particular case it makes no sense) – Denis Wasilew Nov 10 '17 at 21:36
  • Depends on how you are getting the value of `flags`. – Shyju Nov 10 '17 at 21:39
  • It's an array from angular's custom function – Denis Wasilew Nov 10 '17 at 21:42

2 Answers2

7

The whole idea behind using ajax is to give the user the partial page update experience. If you are making an ajax call and once that is done and you are doing a redirect to another page, it does not give the partial page update experience to user. It looks very similar to the normal form submit(full page submit).

If you absolutely have to send the data to server via ajax , but want to do the redirect after the ajax call is successfully finished, you can do that using javascript in the success or done callback event on the $.ajax method.

All you have to do is, set the location.href property to the new url.

var flags = ["aa", "bb", "cc"];

$.ajax({
    url: '@Url.Action("Summary")',
    type: 'POST',
    data: JSON.stringify(flags),
    contentType: "application/json;charset=utf-8"
}).done(function(res) {
     window.location.href = res.newUrl;
}).fail(function(xhr, a, error) {
      console.log(error);
});

This assumes that your server action method returns a JSON response with the newUrl property which contains the url you want to redirect to .

[HttpPost]
public ActionResult Summary(List<string> flagsChecked)
{
    return Json(new { newUrl = Url.Action("Index","Home") });
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
1

One way to do this would be to send the request to the controller via ajax and then render a partial view on your page. The easiest way would be to use the built in ajax helpers in ASP MVC. Here is a link to another post that gives a pretty good overview:

How to render partial view in MVC5 via ajax call to a controller and return HTML