RedirectToAction not refreshing the page as expected
Asked Answered
R

3

7

What am I doing wrong with my MVC code here ? The Index view includes a form that submits to itself, what I'd like is the controller to process the submitted form and then return to the View.

What actually happens is the form is processed correctly, but the View returned is as if nothing happen (e.g. ids that have been deleted are still shown). If I manually refresh the page though, it displays correctly again. I don't think it's broswer caching related, as redirecting to the same view from a different controller works fine. How can I fix it ?

    public ViewResult Index()
    {
        return View(GetComments());
    }


    [HttpPost]
    public ActionResult Index(int[] AllIds)
    {
        if (AllIds != null)
        {
            foreach (int id in AllIds)
            {
               // do stuff
            }
        }

        return RedirectToAction("Index");
    }

Edit: When submitting the form, the breakpoint on the first method is not hit and trying to "Step Into (F11)" the return RedirectToAction("Index"); line just moves straight onto the final } instead.

Recidivism answered 17/12, 2011 at 0:20 Comment(3)
So the POST updates data that is retrieved by GetComments()? After the POST, GetComments() should return different data?Microphysics
Yes, the POST updates/deletes the same data as the GET selects.Recidivism
If you have an async method that isn't marked await, this could be the issueDeadlock
K
5

Install Fiddler or Firebug for Firefox and watch the traffic, see it it really returns a new response or a HTTP 304 from the browser(cached page). If everything checks out then you have a problem with your db persistence and or queries.

Keratoid answered 17/12, 2011 at 0:43 Comment(1)
does this mean if I dont have HTTP 304, means my website is running fine? Because display on gridview doesn't update sometimes, needs another refresh or search to display updated data. Entity results are the culprit then?Spacesuit
M
1

Have you tried this? I'm wondering, depending on how you persist the data, if it's not being saved until after the server returns a response..?

public ViewResult Index()
{ // breakpoint
    var comments = GetComments(); // debug and inspect the value of this variable
    return View(comments);
}


[HttpPost]
public ActionResult Index(int[] AllIds)
{
    if (AllIds != null)
    {
        foreach (int id in AllIds)
        {
           // do stuff
        }
    }

    return RedirectToAction("Index"); // breakpoint
} 

I know some people use an IUnitOfWork in MVC that only calls SaveChanges / Commit on the ORM at the end of the request. Is it possible that the // do stuff removes items from the collection, but does not persist to the db until AFTER the GET Index() is returned?

Update

Instead of return RedirectToAction("Index"), have you tried RedirectToAction(Index())?

Microphysics answered 17/12, 2011 at 0:35 Comment(0)
M
0

Try entering controller name as well. That helped me. For example:

return RedirectToAction("Index","Home");
Merc answered 26/5, 2017 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.