1

Assume we have an action like:

public ActionResult Display(long Id){

  //Do something

return RedirectToAction(//To the Caller)

}

So Display action called by some Views, like:

Index View : @Html.ActionLink("Show", "Display", new { Id=@Model.Id } )

So I need in Display: return RedirectToAction("Index")

Or

Edit View : @Html.ActionLink("Show", "Display", new { Id=@Model.Id } )

I need in Display: return RedirectToAction("Edit")

and so on.

How can we find which action call Display and in the end of the action returned to the caller action? what is your suggestion?

Saeid
  • 13,224
  • 32
  • 107
  • 173

3 Answers3

2

How about passing one more parameter along with id in the ActionLink method?

@Html.ActionLink("Show", "Display", new { Id=@Model.Id ,from="Edit"} )

and

@Html.ActionLink("Show", "Display", new { Id=@Model.Id ,from="Index"} )

and in your action method, accept that as well

public ActionResult Display(long Id,string from)
{

  //Not 100 % sure what you want to do with the from variable value.
   return RedirectToAction(from);
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
1

If you don't want to pass a variable to the redirecting action method, you could also check the Request.UrlReferrer and use that instead.

public ActionResult Display(long Id){

    var caller = Request.UrlReferrer != null 
        ? Request.UrlReferrer : "DefaultRedirect";

    // do something

    return Redirect(caller);
}
danludwig
  • 46,965
  • 25
  • 159
  • 237
1

You could use a returnUrl parameter.

On the caller:

@Html.ActionLink("Show", "Display", new { returnUrl = this.Url.PathAndQuery })

and your Display action all you have to do is redirecting to the returnUrl:

this.Redirect(returnUrl);

This is flexible enough for any other case you might have in the future.

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96