Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, January 8, 2016

ASP.NET MVC 4 - Redirect to the same page after controller ends

ASP.NET MVC 4 - Redirect to the same page after controller ends


From a page I have the following:

@using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, new { returnUrl = this.Request.RawUrl }))  {      @Html.TextBox("IP")      @Html.Hidden("TypeId", 1)        }  

so controller is called correctly:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)  {      // Do some stuff        return Redirect(returnUrl);  }  

My problem is that returnUrl gets null and it does not redirect to the same page that called the controller. Ideas?

Using: ASP.NET MVC 4 Razor

Answer by Felipe Oriani for ASP.NET MVC 4 - Redirect to the same page after controller ends


You could use a Request.QueryString method to get some values from URL, for sample:

@using (Html.BeginForm("AddEntry", "Configure", FormMethod.Get, null))  {      @Html.TextBox("ip")      @Html.Hidden("TypeId", 1)      @Html.Hidden("returnUrl", this.Request.RawUrl)        }  

And in your controller, receive it as a parameter string returnUrl.

Answer by Esteban Elverdin for ASP.NET MVC 4 - Redirect to the same page after controller ends


in your controller class use Request.UrlReferrer. There's no need to pass the url from the page.

   public ActionResult AddEntry(string ip, int TypeId)      {             // Do some stuff             return Redirect(Request.UrlReferrer.ToString());      }  

Answer by Fals for ASP.NET MVC 4 - Redirect to the same page after controller ends


You can get the Refer URL from the Request in the controller:

public ActionResult AddEntry(string ip, int TypeId, string returnUrl)  {         // Do some stuff       string url = this.Request.UrlReferrer.AbsolutePath;         return Redirect(url);  }  

This will redirect you exactly to the calling URL.

Answer by vidriduch for ASP.NET MVC 4 - Redirect to the same page after controller ends


you can also do this if you need to return to something like details page and return to the same page with a query:

return Redirect(Request.UrlReferrer.PathAndQuery);  

Answer by Moaz for ASP.NET MVC 4 - Redirect to the same page after controller ends


1- on Get like Edit(int? id) ViewBag.RefUrl = Request.UrlReferrer.ToString(); 2- on view @Html.Hidden("RefUrl"); 3- on post Edit(int id,string RefUrl) return Redirect(RefUrl);


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.