locked
After RedirectToAction Return Back To The Calling Method RRS feed

  • Question

  • User1185448985 posted

    I have two actions in different controllers, Action1 calling Action2 view.

    Is there a way to come back after seconds to Action1 to continue?

    Action 1:

    //POST - CREATE
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Create(CreateEditInstitutionViewModel model)
            {
             .............................
    _db.Request.Add(request);
    
                    await _db.SaveChangesAsync();
                    return RedirectToAction("ShowMessagePage", new RouteValueDictionary(new { Controller = "MessagePage", Action = "ShowMessagePage", myMsg = "heloo", i = true }));
                    //i want to comeback here to continue
                    return RedirectToAction(nameof(Index)); 
                    
    
            }

    Action2:

    public IActionResult ShowMessagePage(string myMsg,bool isDone)
            {
                if(isDone)
                    return View(new MessageViewModel { msg=myMsg}); // i want to go back to Action1
                return NotFound();
            }

    Sunday, September 27, 2020 8:39 PM

Answers

  • User-474980206 posted

    Then pass the url as a parameter to the show page. It can then redirect back to the original page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2020 9:45 PM
  • User1655654435 posted

    It makes little sense to do it all in the one method like you do.

    If you have a message you want to display after the operation, you probaly should pass the message to the page you want to go to after.

    so if you want to go to the index page, then send the message to the index page and display it there.

       return RedirectToAction(nameof(Index), new { message = "created something awesome!"}); 
    } public IActionResult Index(string message = null) { ViewBag.Message = message; return View(); } in view: @if(ViewBag.Message != null) { <p>ViewBag.Message</p> }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 28, 2020 10:28 AM

All replies

  • User-474980206 posted

    Then pass the url as a parameter to the show page. It can then redirect back to the original page.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 27, 2020 9:45 PM
  • User1312693872 posted

    Hi,Amani Ai

    If you want to get the message and then go back to index after a few seconds , why you do not just finish this in action1? Because the parameters of action2 are all from action1, you can merge them together.

    and the way to set the interval is to use js in ShowMessagePage View:

    @model MessageViewModel
    
    @Html.DisplayFor(m => m.msg)
    
    @section Scripts
    {
    <script>
        $(document).ready(function () {
            window.setTimeout(function () {
                window.location.href = '/MessagePage/Index';
            }, 1000);
        });
    </script>
    }

    Best Regards, 

    Jerry Cai

    Monday, September 28, 2020 5:51 AM
  • User1655654435 posted

    It makes little sense to do it all in the one method like you do.

    If you have a message you want to display after the operation, you probaly should pass the message to the page you want to go to after.

    so if you want to go to the index page, then send the message to the index page and display it there.

       return RedirectToAction(nameof(Index), new { message = "created something awesome!"}); 
    } public IActionResult Index(string message = null) { ViewBag.Message = message; return View(); } in view: @if(ViewBag.Message != null) { <p>ViewBag.Message</p> }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 28, 2020 10:28 AM