1

I have passed the Model object from the View to controller action method through the AJAX post call. Inside the action method, we have a some business logic to be perform based on the received input fields. if business logic fails, then we will return the error message to user else, then we need to redirect the user to some other page. in this case, Redirection to other page is not working in the AJAX post call. Could you please provide me the any alternative approach on this? Note:- i need to perform this post operation in the AJAX call. Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
vellaichamy
  • 143
  • 1
  • 10
  • Possible duplicate of [RedirectToAction with Ajax.Beginform , unexpected results](http://stackoverflow.com/questions/23786838/redirecttoaction-with-ajax-beginform-unexpected-results). – Zabavsky Jul 10 '14 at 10:04
  • Did the below help you at all? – Zach M. Jul 16 '14 at 20:00

1 Answers1

0

You can simply check if the status is an error and if so redirect in your ajax call

$.ajax({
       url: '/ThingToDo/',
       type: 'POST',
       contentType: 'application/json; charset=utf-8',
       data: data,
       success: function (status) {
       if (!status.error) { alert(!status.error); }
       else { window.location.href = "/Redirecthere/"; }
       }
Zach M.
  • 1,188
  • 7
  • 22
  • 46
  • 1
    Thanks. It's much more helped. i am doing the redirection from the AJAX post call by using this method. could you please let me know, is this a best way to do the redirection or do we have some other way? if yes, please share it – vellaichamy Jul 17 '14 at 11:32
  • This way is perfectly fine if your redirect needs to be handled after some logic performed by you call, if not, you could always do the redirect from your controller method. The issue that I ran into was that if you redirect from the controller the post never returned anything to the ajax call. Remember to mark this as answered for others to benefit from the question. – Zach M. Jul 17 '14 at 12:52