1

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());

    }
tereško
  • 58,060
  • 25
  • 98
  • 150
kicked11
  • 117
  • 8
  • why don't you just append querystring to detect from where it is come ? – Vishal Sharma Apr 14 '14 at 07:50
  • how? got a code example? – kicked11 Apr 14 '14 at 07:51
  • http://stackoverflow.com/questions/3100923/how-to-pass-query-string-parameter-in-actionlink-in-mvc just add any anon. variable to make it different and then detect in your post method , if it does exist redirect to something else redirect to other one... – Vishal Sharma Apr 14 '14 at 07:54

2 Answers2

1

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);
    }
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • in the DeleteConfirmed model.PreviousUrl = null i think its because i do this : answer answer = db.answer.Find(id); in DeleteConfirmed so it doesn't take the previousurl property? – kicked11 Apr 14 '14 at 08:32
  • what I did is add the previuous as a parameter public ActionResult DeleteConfirmed(int id, Uri PreviousUrl) but I get this error now: a potentially dangerous request.Path... any idea? – kicked11 Apr 14 '14 at 08:39
  • Please recheck. I have updated my answer. see first step. you have to create new field for address model. Aslo, you can't pass url to the controller's action due to the type of date. this is why you get error "a potentially dangerous request.Path" – Ashwini Verma Apr 14 '14 at 09:04
  • Good to hear that. :) – Ashwini Verma Apr 14 '14 at 09:06
  • I edited your sulotion in my answer the only thing I had to change was add the Uri parameter and use redirect instead of RedirectToAction. Thanks A lot!! – kicked11 Apr 14 '14 at 09:08
0

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

Community
  • 1
  • 1
Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36