3
votes

I'm trying to get my app to redirect to the previous page after it updates the database but I can't seem to figure it out. What's complicating it is that the previous page isn't just a simple page. It's populated with information pulled but a specific row in the database so the URL has

blah blah blah.../ProjectDetails?id=(the ID number)

I've tried using the Redirect Methods but they aren't as straight forward as I'd hoped. Can someone help me figure out how to go back to the previous page?

View

        <form method="post">
            <select class="custom-select mb-4 col-6" name="Assignee">
                <option>John</option>
                <option>Jane</option>
                <option>Jim</option>
                <option>Juju</option>
            </select>
            <br>
            <button type="submit" class="btn btn-info">Save</button>
            <a class="btn btn-danger" asp-page="./Teamlead">Cancel</a>
        </form>

Code Behind

public async Task<IActionResult> OnPostAsync(int id)
        {
            Submission = _SubmissionContext.Submissions
                            .Where(s => s.ID == id).First();
            Submission.AssignedTo = Assignee;
            await _SubmissionContext.SaveChangesAsync();

            return RedirectToRoute("./ProjectDetails"); //PROBLEM HERE

        }
2

2 Answers

10
votes

You need to pass the id to the redirected page. You can use an anonymous type to do that like:

public async Task<IActionResult> OnPostAsync(int id)
    {
        Submission = _SubmissionContext.Submissions
                        .Where(s => s.ID == id).First();
        Submission.AssignedTo = Assignee;
        await _SubmissionContext.SaveChangesAsync();

        return RedirectToRoute("./ProjectDetails", new { id = Submission.ID });
    }
1
votes

You should try decorating your razor page view at the top with your route attributes like so:

@page "{id}"

The following link below should help example more about routing parameters:

https://www.mikesdotnetting.com/article/310/routing-in-razor-pages