1

I have a form, that can be reached from 5 different pages. The form has save and finish buttons.

Save button - submits form (does the POST request) and then reloads the same page (GET request) Finish button - submits forms (POST request) and should go to the one of those 5 locations from which the form was reached.

I have tried to use the TempData and save the location on the form GET request, looks fine until I use the Save button, because when it reloads page, TempData url is the same (current) url.

I believe jQuery has something like window.history.back and skips those reloads, but is that possible, to do that immediately on submit button and post, without using jQuery? Maybe there is possibility to do the history check for previous different page in MVC Controller (I mean to skip reloads and get a first different url)? Any help is greatly appreciated.

1 Answer 1

0

Your method using a GET parameter seems good. I doesn't even need javascript which is good because I have always been told that history.go is the devil. Why can't you pass the parameter unchanged if the user click save, so that you don't loose the referring url ?

I would do the following:

  • user requests form.html?referring=origin.html
  • server returns the form with a hidden field containing the referring address
  • on Save the form is submitted and the hidden field is kept so that we don't loose the information
  • on Finish the form is submitted and the user is redirected to the referring url

I am no security expert but you might want to check/escape the referring url, to avoid a poor man's xss.

6
  • But how can I keep that "unchanged" url of original? when I click save, it refreshes page and previous url becomes the same form url... or I missed some idea here
    – K V
    Aug 12, 2015 at 8:07
  • Maybe I'm simplifying too much how your application works. I don't see the point of submitting the form using a POST request then refreshing the page with a GET request. You can do both with a single POST request. When handling the form, your controller can retrieve the original url from the hidden field and add it to the new form of the refreshed page. Aug 12, 2015 at 8:38
  • I realize that since you have two submit actions, you must be using javascript to submit your form. You can use simple buttons for the 'Save' and 'Finish' actions. When the button are clicked, you change the form's action using $('#form').attr('action', 'correspondingUrl'); and then submit the form using $('#form').submit(), that way you should not have to make a second request to refresh the page. If you are attached to that second request, you can refresh the page using window.location.href = 'form.html?referring=origin.html' instead of simply refreshing the page. Aug 12, 2015 at 9:00
  • yes, this is with jQuery, what I have mentioned in question, but isn't there any MVC way in controllers, viewbags or so to deal with this problem? I may change as little as possible in this app, so making simple buttons and binding ajax call for submitting will change code a little bit. I will do this only if there is not any other way to do that.
    – K V
    Aug 12, 2015 at 9:11
  • This problem isn't related to the MVC model. If you don't want to change your app too much (which is understandable), I have no other idea than what I already gave you :/ IMO the best design for what you want would have been to use a modal window to display the form (never leave the referring url) and simply close it when the form is submitted using the 'Finish' button. I upvoted your question, hopefully someone else will be able to better help you. Aug 12, 2015 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.