Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Page was redirected from RedirectToAction() method

Here are my ActionResult methods :

    [HttpGet]
    public ActionResult Index(string cityID, string numbers, int days, bool onlySpecial)
    {
        LastTwoParameters lastTwoParameters = new LastTwoParameters();
        lastTwoParameters.listCities = Common.GetDropDownCitiesList();
        lastTwoParameters.listLastTwoResult = new List<getReport_LastTwo_Result>();

        // if IsPostBack , then execute

        if (!string.IsNullOrEmpty(cityID) && days > 0)
        {
            using (KQXS context = new KQXS())
            {
                lastTwoParameters.listLastTwoResult = context.getReport_LastTwo(cityID, numbers, days, onlySpecial).ToList();
            }
        }


        return View(lastTwoParameters);
    }

    [HttpPost]//Run action method on form submission
    public ActionResult Index(List<Cities> listCities, string cityID, string numbers, int days, bool onlySpecial)
    {
        return RedirectToAction("Index", "LastTwo", new{cityID = cityID, numbers = numbers, days = days, onlySpecial = onlySpecial});
    }

What I need is to detect if the page is called from a RedirectToAction method. Because I don't have the need to calculate the result the first time the user open the page.

like image 319
NeedAnswers Avatar asked Oct 20 '25 11:10

NeedAnswers


1 Answers

There are two ways to resolve your issue.

  1. You could use service side caching. Controller has TempData property. Simply set to the TempData["wasRedirected"] = true. And after your first read from the framework will clear the value. In your case TempData["wasRedirected"] == null means that your code was not redirected.

  2. You could use url params for example you action:

    public ActionResult Index(bool? wasRedirected)
    {
        return View();
    }
    

    And in redirect method you will have code like this:

    [HttpPost]
    public ActionResult Redirect()
    {
        return this.RedirectToAction("Index", new { wasRedirected = true });
    }
    
like image 123
Pavel Avatar answered Oct 25 '25 04:10

Pavel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!