kicked11
kicked11

Reputation: 117

MVC Return to specific link

How do i return to a specific link depending on where I came from?

I have a admin panel, and this allows to delete an answer from 2 different places. So now i want to be able to redirect to user to the link he came from

so these are the links:

/Answer/Index -> /Answer/Delete/{id}
/Statement/Edit/1 -> /Answer/Delete/{id}

I've done some research and found this

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

so i used it in my controller

  public ActionResult Delete(int id = 0)
    {
        answer answer = db.answer.Find(id);
        if (answer == null)
        {
            return HttpNotFound();
        }
        return View(answer);
    }

    //
    // POST: /Answer/Delete/5

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        answer answer = db.answer.Find(id);
        db.answer.Remove(answer);
        db.SaveChanges();
        return Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString());
    }

the problem is that this redirect to /Answer/Delete/{id} instead of the link right before that. How can I do this??

SOLUTION thanks to Ashwini Verma

the solution of Ashwini Verma works after making this changes

 public ActionResult DeleteConfirmed(int id, Uri PreviousUrl)
    {
        answer answer = db.answer.Find(id);
        db.answer.Remove(answer);
        db.SaveChanges();
        return Redirect(PreviousUrl.ToString());

    }

Upvotes: 1

Views: 563

Answers (2)

Ashwini Verma
Ashwini Verma

Reputation: 7525

You should have got Request.UrlReferrer in Delete() insted of DeleteConfirmed(). Follow this step.

Create a new field called PreviousUrl for Address Model.

public class Address
{
    ...
    public string PreviousUrl  { get; set; }
}

Hold the previous URL in the view model.

    public ActionResult Delete(int id = 0)
    {
        Answer answer = db.answer.Find(id);
        if (answer == null)
        {
            return HttpNotFound();
        }

        // get the previous url and store it with view model
        answer.PreviousUrl = System.Web.HttpContext.Current.Request.UrlReferrer;

        return View(answer);
    }

Add it to hidden field

    @using (Html.BeginForm())
    {
        ...
        <input type="hidden" name="PreviousUrl" value="@Model.PreviousUrl" />
    }

Redirect based on Model.PreviousUrl

    public ActionResult DeleteConfirmed(int id)
    {
        return RedirectToAction(model.PreviousUrl);
    }

Upvotes: 1

Vishal Sharma
Vishal Sharma

Reputation: 2793

A Very Simple Solution is to generate your outbound url link with extra querystring parameter to identify from which page it is ,

you can use enum for page or any hard code value also

example of querystring append on action link

will help you to generate action link with query string parameter

 <%= Html.ActionLink("Check this", "Edit", "test", 
    new { page = 2 }, new { style = "display:block" })%>

and on your post method ,

see for page parameter and redirect accordingly

Upvotes: 0

Related Questions