Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I need to implement BACK button feature on each of my pages. System is already having NEXT button to visit next view. I am using @using (Html.BeginRouteForm() with target action and controller set, inside which my NEXT button (<button type='button' id='NextButton'>NEXT </button>) has been placed. All the views are strongly-typed views.
Now, for implementing BACK button, I added BACK button and in jQUERY I attached controller action method of the previous View to call. The code is like:
PHP
$("#BackButton").click(function () {
                $.post("/PreviousController/PreviousAction/");
            });


When clicked on BACK button, the previous controller and action is getting invoked properly but the previous view is not getting rendered. It still shows the current view only.

Please provide some suggestions on this.

Thanks.
Posted
Updated 8-Dec-11 23:29pm
v2

1 solution

All you're doing is posting to a controller action, you haven't defined a success handler to determine what to do with the results of that call.

I take it that /PreviousController/PreviousAction/ works out what the last action called by the user was?

In which case, why not just make that part of your model when rendering your views?

C#
public class SomeModel
{
    public string LastAction {get; set;}
}



C#
public ActionResult SomeAction()
{
    var model = new SomeModel();
    model.LastAction = GetPreviousAction(); // Make this a method in some class that always returns the controller / action
    return View(model);
}


Then in your Views, you can just render out links \ buttons

HTML
@model MyProject.SomeModel
<div class="my-view">
   <a id="my-link" href="@Model.LastAction">Back</a>

   <input type="button" name="backButton" id="backButton" value="Back" onclick="window.location.href='@Model.LastAction'">
</input></div>


Or, if you want to stick with what you've got, have your /PreviousController/PreviousAction/ return some JSON of the controller\action to navigate to. Your script could then look like


JavaScript
$("#BackButton").click(function () {
    $.post("/PreviousController/PreviousAction/", function(data) {
         // you might need to parse the results with $.parseJSON(data) etc..         
         window.location.href = data;
    });
 });



Personally, I'd make the 'LastAction' part of my model so I could just render out links \ buttons without requiring a AJAX call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900