1

I have a controller with 'Details' action and 'Edit' action. I navigate to Edit using View(); then a page with a partial view is rendered.

In the partial view there is a form:

@using (Html.BeginForm("CreateOrEdit", "Licenses", FormMethod.Post, new { @class = "pure-form pure-form-aligned", id = "change", onsubmit = "process(this);" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="floatLeft width100per">
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.CustomerId)

        <button onclick="goBack()" type="button" class="pure-button pure-button-primary newButton">Back</button>
        <input type="submit" value="Save" class="pure-button pure-button-primary newButton marginRight10" onclick="getModules(); setGuid('@Guid.NewGuid()');" />
        <div class="floatLeft pure-form">

goBack() function:

function goBack() {
    history.go(-1);
}

If I navigate to Edit and press the back button, everything's fine. But if I press the Save button, form is being submitted and CreateAndEdit action is called - still OK.

The CreateAndEdit action uses:

RedirectToAction("Details", new { id = license.Id });

in order to navigate to Details page again.

Then, if I go to "Edit" again and then press Back - CreateAndEdit action is called again - Not OK!

How can I always go back to "Details" page without calling CreateAndEdit when Back is pressed.

3
  • Define as anchor ( <a ) to go to defined url route instead of replying to browser history. Commented May 3, 2018 at 8:31
  • @ldanLevi You can try to use Ajax call as shown on Can't call function using ajax and inform me if it is working for you. Commented May 3, 2018 at 9:08
  • @MuratYıldız It can work, but it's not the same as going back.. Commented May 6, 2018 at 6:51

2 Answers 2

2

You need to disable the cache for the page you don't want a user to be able to go back to due to form submission, etc.

You could try decorating the route with

[OutputCache(NoStore=true, Duration=0)]

I believe [NoCache] will also work as a decoration on your controller action.

Sign up to request clarification or add additional context in comments.

Comments

1

I think the behavior you are seeing is standard browser behavior. After you have submitted a form, if you then press the "Back" button of the browser (which is the same as history.go(-1);), the form will be resubmitted.

As a workaround, if the "CreateOrEdit" page is only accessible from the "Details" page, just redirect to the "Details" page directly instead of going through the browser history.

function goBack() {
    location.href = '@Html.Raw(Url.Action("Details", "Licenses",  new { id = Model.License.Id }))';
}

1 Comment

Thanks, I thought of it too.. But I'm looking for another solution in order to maintain the history as it is.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.