James123
James123

Reputation: 11652

Redirecting to previous view from different views or controllers in MVC4?

I have same view from Users level and Staff level. When User login into application he sees his activities and creates new activity from Create view. From Staff they will search users and see user's information then creates new activity behalf of user in same Create view.

tricky is here, both are coming to different view to see Create view. When they cancel and save they have to go back to previous page.

How to handle this?

ex:
http://localhost/members/listActivities - for members
http://localhost/staff/memberinfo

To 

http://localhost/members/create

Upvotes: 0

Views: 2730

Answers (2)

Hamid Reza
Hamid Reza

Reputation: 2973

You may add a property to your UserViewModel named IsStaff.It would be a boolean one.for staff users it would be true,else it would be false.Then after doing your creation function you will get your current user's IsStaff value and check it in an if condition and redirect them to diffrent actions.

public bool IsStaff{get;set;}

And for Create:

public ActionResult Create()
{
    ...//do creation here
    if(UserRepository.GetUserByUsername(User.Identity.Name).IsStaff==true)
        return RedirectToAction("staff","memberinfo");
    else
        return RedirectToAction("members","listActivities");
    return View();
}

Upvotes: 0

David
David

Reputation: 218847

For the "cancel" action you could skip the server-side involvement entirely and use JavaScript to direct the browser to its previous location. (Which has the added benefit of supporting anybody who comes there from yet another location in the future.) Something like this:

<input type="button" value="Cancel" id="cancelButton" />
<!-- and later... -->
<script type="text/javascript">
    $('#cancelButton').click(function () {
        window.history.back();
    });
</script>

For the "save" action, can you discern on the server which "type" of user they are and direct them accordingly? If not, perhaps you need to track where they came from when the Create action first responds with the view. Perhaps the action could accept a redirect as a parameter? Something like this:

public ActionResult Create(string redirectUrl)

Then you could add that parameter to the Create view model, storing it in a hidden field or something of that nature. And when you post back to the Create action on save you would include it again in that parameter set and use it for the redirect (defaulting to a specific page if it's empty, of course).

Then whenever you have an @Html.ActionLink to Create you'd include a redirectUrl as a route value.

Edit: For completeness, as mentioned by Oliver in a comment below, you could attempt to discern the previous page in a controller action by Request.UrlReferrer. This would be used in the non-post call to Create to auto-fill the hidden field that will be returned in the post call. (Since in the post call the referrer is the page itself, so that wouldn't be helpful.) Keep in mind two main things here, though:

  1. Browsers control the referrer, you don't. Some browsers may send bad referrers or, more commonly, none at all. It might work 9 times out of 10, it might not. But, again in the interest of completeness, it can be used as a fallback to catch cases where you (or another developer) don't explicitly include a redirectUrl as a route value.
  2. This couples the controller to the HTTP context. That might never be a problem for your needs, but the first thing it breaks is unit testing of that action method. It's best to avoid this kind of coupling unless absolutely necessary, and to know exactly what you're doing when you use it.

Upvotes: 2

Related Questions