I encountered an issue with redirecting after submitting a form in our mobile site running off jQuery Mobile and ASP.NET MVC.
Problem was, after calling
RedirectToAction(...), the browser’s URL bar would not update correctly. Instead of showing the destination URL after the redirect, it showed the URL from the form.
Befuddled, I guessed that someone must have encountered this issue. I came across a few related issues involving redirects using PHP and Rails, but this Github Issue for jQuery Mobile led me to a solution.
User tenor’s comment explained a good .NET-ish solution. By telling the master page to ask for the data-url attribute from ViewData, Controllers can set the
data-url attribute of jQuery Mobile’s page div -
I was happy with that answer - it gave us a nice control in all of our actions to set the browser URL to whatever we would like. Our webapp is distributed to a lot of different users for a lot of different businesses, so I thought this would be a useful tool to have in the future.
However, my teammate found a simpler solution. By disabling ajax in the form, jQuery Mobile no longer uses ajax to submit the form, but rather hands control over to the browser to submit, which then plays nicely with the redirect. One way to do this is by letting jQuery know before creating the form like so:
$.mobile.ajaxEnabled = false; . That’s a lot less code than the .NET-ish solution from earlier. Nice.