Peter
Peter

Reputation: 115

Asp.net mvc refresh page after HTTPPOST

I have problem with ASP.MVC. I call this action method from many page and I need after it refresh page, but I do not have link to page where user was.

public ActionResult Hide<T>(T item, User user) where T : IHidable
{
    //... Some action
    return SomeThingWhatINeedAndRefreshPage();

    //Now I use this, but I do not want to redirect user to Home
    //return RedirectToAction("Index", "Home");
}

Upvotes: 2

Views: 4351

Answers (3)

MikeTeeVee
MikeTeeVee

Reputation: 19420

This worked for me to retain my Query String:

return Redirect(Request.UrlReferrer.ToString());

I based this off of a comment left by @YuriyP and decided to make it a full Answer.
This probably breaks some pattern for development, because you may want to asynchronously update part of your page without a full postback.

Upvotes: 0

su8898
su8898

Reputation: 1713

You can get the action and controller names like below and call RedirectToAction with these values.

string actionName = this.ControllerContext.RouteData.Values["action"].ToString(); string ctrlName= this.ControllerContext.RouteData.Values["controller"].ToString();

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1039538

One possibility would be to pass as additional parameter the original page where the client was:

public ActionResult Hide<T>(T item, User user, string returnUrl) where T : IHidable
{
    //... Some action

    return Redirect(returnUrl);
}

Now when you are building the url to this controller action simply include the returnUrl parameter from the current request.

Upvotes: 1

Related Questions