Let's say I have a controller action that deletes an item out of a user's shopping basket. This controller action is triggered by performing a POST to the url ~/delete/{id}. If I have several pages on my application that will post to this url, how do I construct the controller action to redirect back to the page that posted to it?
RedirectToAction(String, String, RouteValueDictionary) Redirects to the specified action using the action name, controller name, and route values.
Adding View to Controller in Asp.Net MVC Here is Project view after adding all View related to Action Method in MainController. Now on Index view, let's add Hyperlink. To add Hyperlink, we need to begin with @html helper with following Action Link, then we need to provide linkText to display and ActionName.
You should provide a RedirectToUrl parameter from the posting page.
Relying on referrer headers is not a good practice.
Instead, do something like this:
public ActionResult Delete(int id, string RedirectToUrl)
{
// check if RedirectToUrl is null or empty and redirect accordingly
}
On the posting view or partial view you can provide the parameter in several ways:
<%= Html.Hidden("RedirecToUrl","/my/lovely/url") %>
or
<form action="/item/delete/22?RedirectToUrl=/my/lovely/url">
I'd prefer the first option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With